Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing Value from 1 class 2 another.  RSS feed

Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All

I m a new user of java.

plz help me out 2 solve my problem.

1 class = dbconnection
2 class = util
3 class = company
4 class = login

i ve connected database with dbconnection in login form using
dbconnection conn = new dbconnection();

then userid is asked. When i enter my login it passes value to util class
util u = new util(); // this is initialized at top.

public String setValue(String str)
{ String myVal
myVal = str;
return myVal;

when i m trying to print that value i is showing user id.

I ve a button on login form called "Company".
I ve inititlized util object at the top.

util abc = new util();

i want to get the value that i ve assigned in my login form using setValue() method.

But when ever i m getting value from util class it shows null value;
Plz tell where i m wrong?

author and iconoclast
Posts: 24217
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see two major problems here:

1) Each time you call "new util()" you're creating a new "util" object. Each "util" object has its own copy of any member variables in the class. Calling setValue() on one util object will have no effect on the return value of getValue() in another util object. You want to share a single util object between your various classes, by passing it around as a method argument and/or storing it as a member variable in your various classes.

2) The version of "setValue" you've shown us here actually assigns a value to a local variable named myVal -- a variable that's used only in the setValue method. If there's also a member variable named myVal, then that member variable will remain null even within a single object after setValue is called.

These are really fundamental Java issues and have nothing to do with Swing/AWT as far as I can see, so I'm going to move this thread to the Java in General (Beginner) forum.
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically a "setter" method (like setValue() here) doesn't need to return anything. You can return just void instead.

I hope Ernest has explained the other issues so that you can understand. If you get stuck trying to fix things, please let us know and we'll be glad to help.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!