| Author |
questions about variables
|
Joe Panully
Greenhorn
Joined: Oct 10, 2004
Posts: 22
|
|
I know I ask a lot of questions but that's because I'm awesome, awesome to the max. Anyhow... I have a public method where I declare some variables I then have a private method that i'm trying to pass these variables to example private char superpants(one,two,three,four){ blablablablabla; } Where one, two, three, and four were varibales I declared in another public method, can I do this? How do I get this method to recognize those variables as the ones I declared in the other method. I am having a really hard time grasping the organization of java coming from a perl background .  [ October 27, 2004: Message edited by: Joe Panully ]
|
 |
C. Magmanum
Ranch Hand
Joined: Apr 03, 2004
Posts: 35
|
|
I think u need to specify the variable with its full name. ...dont know if that's what u are after
|
 |
Jimmy Die
Ranch Hand
Joined: Nov 20, 2003
Posts: 97
|
|
Hi, If you declare your variables within your method then they are restricted to your method because of "Variable Scope". Here is the difference public void yourMethod() { int i = 0; } can't find i outside of the brakets {} but if we do this int i; public void myMethod() { i = 0; } we can see what i is because it was declared outside of the method...
|
Jimmy Die
|
 |
Joe Panully
Greenhorn
Joined: Oct 10, 2004
Posts: 22
|
|
More accurately my question is this: I need to pass a variable that I used/declared somewhere to another method. Everytime I try this it tells me I need an identifier, which means it wants me to re-declare it I think. Here is a basic example. public class hello{ int x=5; public int tomato(x){ int y=x*5; return(y); } }
|
 |
nilesh Katakkar
Ranch Hand
Joined: Oct 27, 2004
Posts: 35
|
|
Yep it will do that. Let's see if I can explain. public class hello{ int x=5; // Here x is declared and initialized as an instance variable or // member variable for this class. This variable is accessible to you // anywhere in your class .. public or protected or private methods // In following function you can x directly and do not have to declare it // again. public int tomato(){ int y=x*5; return(y); } // Now if you wanna define your function such as it takes one input // parameter you will need to define its type. Perl is a scripting language // so you dont need to define the type. Java is a TYPED language.. it // requires type information for every variable you use at compile time. public int tomato(int x){ // In this function if you use x it will be the value of x that // you are passing to this function. // e.g if you call this function as tomato(10) then int y=x*5; // will be 50 not 25 // If you want to use your memeber variable or instance variable x // that you defined previously then int z = this.x * 5 ; // will be 25 //Since you redeclared variable x in this methods formal parameter, it // takes precedence in this methods scope. // Using this.x we tell compiler to use the variable in class scope... } } Hope this helps. More accurately my question is this: I need to pass a variable that I used/declared somewhere to another method. Everytime I try this it tells me I need an identifier, which means it wants me to re-declare it I think. Here is a basic example. public class hello{ int x=5; public int tomato(x){ int y=x*5; return(y); } }
|
nilesh<br />neilindallas@hotmail.com
|
 |
 |
|
|
subject: questions about variables
|
|
|