Home Forums Gamescan Chat42 About
* Login   * Register * FAQ    * Search
It is currently Wed 06-25-2025 9:38AM

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: help with java
PostPosted: Wed 06-07-2006 3:44PM 
Offline
Sergeant

Joined: Thu 01-13-2005 5:32PM
Posts: 45

Source: Off Campus
if anybody can help i would appreciate it

how can i access a varible that is in a listener in another class..for example

public class aClass....extends jdialog{
public int aVar=0;
private jbutton but1=new ....

private void jbInit...{
....
but1.addacttionlistenr(.....{
public void actionperformed....{
function1(e)....
}
}
);
........
}

public void function1(ActionEvent e) {
aVar=value In Input Box When button but1 is clicked;
}

}

now if i was trying to access the aVar in another class how would i do it...because right now im doing this and its giving me 0 instead of the new updated value.

public class bClass...{

aClass instance=new aClass();

System.out.println(instance.aVar).......prints 0 instead of the updated value;
}



i will greatly appreciate any help
THANKs


Top
 Profile  
    
 Post subject:
PostPosted: Wed 06-07-2006 4:35PM 
Offline
Captain

Joined: Tue 06-01-2004 3:57PM
Posts: 136

Source: Off Campus
It looks like you're doing it correctly. If it was wrong, the compiler would yell at you for trying to access something that was private access only.

When you are calling function1 to change the variable, are you sure it's in the same instance as the one you created in the other class? Because in your example

aClass instance=new aClass();

System.out.println(instance.aVar).......prints 0 instead of the updated value;
}

all you're doing is creating a new instance, then printing a variable from it. Since the method to change the variable was never used for that instance, it prints 0, which you have set as the default for the variable.

If that's not the problem you could send me your full code and I could see what I can do with it.


Top
 Profile  
    
 Post subject:
PostPosted: Wed 06-07-2006 4:53PM 
Offline
Sergeant

Joined: Thu 01-13-2005 5:32PM
Posts: 45

Source: Off Campus
oops that was not the updated code....I added
"bClass instance=new bClass(aVar);" to function 1 in classA

and i wrote a constructor in classB, well this is how classB looks


public class bClass...{
public int aVar=0;

public bClass(int aVar){
this.aVar=aVar;
}

System.out.println(aVar)


and this gives me 0


Top
 Profile  
    
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group