This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Value from 1 class 2 another.

 
Anand Karia
Ranch Hand
Posts: 156
Java
  • 0
  • 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.
u.setValue(txtUserId.getText());

public String setValue(String str)
{ String myVal
myVal = str;
System.out.println(myVal);
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();

Problem:
========
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?

ANAND
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • 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.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic