Home arrow static arrow Java Programming [Archive] - how do you make variable work everywhere?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how do you make variable work everywhere?
This topic has 15 replies on 2 pages.    1 | 2 | Next »

Posts:343
Registered: 7/13/04
how do you make variable work everywhere?  
Jul 15, 2004 10:49 AM



 
public class MessageID {
public MessageID(){
try {

//lots of stuff

String variable = " ";

//lots of stuff

}catch { blablabla}
}
}
what do have to type in front of it to make that String variable available for use by other clases (within the same package).? thnx
 

Posts:18,384
Registered: 21.03.00
Re: how do you make variable work everywhere?  
Jul 15, 2004 10:55 AM (reply 1 of 15)



 
Hi,

You should move it out of the method, and make it a class attribute. It will be available for other classes within the same package, if you don't write anything in front of it.

/Kaj
 

Posts:31,095
Registered: 4/30/99
Re: how do you make variable work everywhere?  
Jul 15, 2004 10:56 AM (reply 2 of 15)



 
Local variables (the ones declared inside a method) are only available to code inside that method, and only when that method is running.
 

Posts:343
Registered: 7/13/04
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:05 AM (reply 3 of 15)



 
ok, but the:
String variable;
gets its value in part from another "variable" (variable2) wthin the method, and when i take variable out it thinks the variable2 dosn't exist anymore becuase variable2 isn't declared and given a value until the method takes place
 

Posts:3,258
Registered: 00-08-28
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:07 AM (reply 4 of 15)



 
I would suggest some reading on scope of variables.
 

Posts:370
Registered: 8/17/02
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:22 AM (reply 5 of 15)



 
public class foo
{
protected static String bigFoo = "Big Foo";
}
---------------next class----------------------
public class foo2
{
public static void main(String args[])
{
System.out.println(foo.bigFoo);
}
}

Protected... means they have to be in the same package... as you requested.. public means they are available to all out side package.. private means they are available to only that class..
 

Posts:21,719
Registered: 98-02-20
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:24 AM (reply 6 of 15)



 

Scope should be kept as narrow as possible. Making it available all the time to all the classes in a package sounds like it could be a very bad idea to me. JMO.

%
 

Posts:957
Registered: 3/31/04
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:25 AM (reply 7 of 15)



 
public class MessageID {     String variable = nullpublic MessageID()    {        try         {           String  variable2 = "michael jackson";            variable = variable2;        }        catch(Exception e) { ; }    }}
 

Posts:343
Registered: 7/13/04
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:39 AM (reply 8 of 15)



 
public class MessageID{ String variable = null; public MessageID(){try{String  variable2 = "michael jackson";variable = variable2;}catch(Exception e) { ; }}}

----------------------------------second class------------------------------------
but this does not work: ( and i need it to work)

public class Michael{   public Michael {      try {          String Variable3 = variable;     }catch(Exceptione e) {}   }}
 

Posts:3,258
Registered: 00-08-28
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:44 AM (reply 9 of 15)



 
now to make it work you have to understand whats it that you are doing wrong. Again I would suggest going through the scope of the variables.

Secondly when you are assigning a String a variable value have you initialized that variable and assigned it a value already.
 

Posts:370
Registered: 8/17/02
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:49 AM (reply 10 of 15)



 
hahah that is cause ur a *****
you gotta make it static and you gotta include a class name.....
if it is not static you have to work with instances... mine was a better way
 

Posts:343
Registered: 7/13/04
Re: how do you make variable work everywhere?  
Jul 15, 2004 11:57 AM (reply 11 of 15)



 
i did make it protected static. good idea. the problem though, when the first class runs it's method, i dont think it saves the member variable as its new value, because when the program goes on to run the second class

String variable3 = MessageID.variable;

System.out.prinln(variable3);

variable 3 is a null value
 

Posts:3,055
Registered: 18/06/98
Re: how do you make variable work everywhere?  
Jul 15, 2004 12:15 PM (reply 12 of 15)



 
how do you make variable work everywhere?
Give him/her a mobile phone (or an iDEN digital radio) and a notebook computer with a wireless connection. With enough networking infrastructure the variable can work everywhere - even when taking a cab.
 

Posts:957
Registered: 3/31/04
Re: how do you make variable work everywhere?  
Jul 15, 2004 12:27 PM (reply 13 of 15)



 
add getters and setters for MessageID then

MessageID mid = new MessageID();String localvar = mid.getVariable();


if you are not going the public/protected route.
 

Posts:343
Registered: 7/13/04
Re: how do you make variable work everywhere?  
Jul 15, 2004 12:32 PM (reply 14 of 15)



 
duh! no wonder it was giving me troubles this is what i did:

public class hi {

protected static String variable = null;

public hi() {

String variable = "hi";

}
}
forgot to erase the second declaration, so for whatever reason it was giving variable a null value like that. once i erased the second "String" word now it works fine
 
This topic has 15 replies on 2 pages.    1 | 2 | Next »