Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

functions / subroutine 1

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
I thought transferring skills from one programming language to another was supposed to be easy, but Java has me baffled.

My biggest problem is being unable to create a working subroutine [sadeyes]

The code below does obviously not reflect my real objective, but pointing out all wrong assumptions would be appreciated.

Code:
public class whatever {

String global = "world";

public static void main(){
mySub("hello");
}

public static void mySub(String x){
System.out.print(x+" "+global);
}

}

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
2 things:
1-global has to be "static" because you use it in a static method
2-"main" needs to have "args" as an argument:
Code:
public class frmtst {

    static String global = "world";
    
    public static void main(String args[]){
        mySub("hello");
    }
    
    public static void mySub(String x){
        System.out.print(x+" "+global);
    }

}



_________________
Bob Rashkin
 
Oh, cheers! I am home now and will try again with your input tomorrow. It had been my assumption that mistakes lay with the layout of mySub.

Thanks,

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Stormbind, what language are you migrating from? When you say things like 'subroutine' instead of 'method' it tends to indicate you are coming from a non-Object Oriented language.

If this is the case, and you want to make the most out of Java, you'd be advised (at some point) to read around the subject of Object Orientation in general. Google should give you thousands of links.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
No, I have used OOP languages before - it's just the small syntax differences that are throwing me :(

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top