| Author |
Passing Value from 1 class 2 another.
|
Anand Karia
Ranch Hand
Joined: Sep 25, 2004
Posts: 154
|
|
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
|
Love is GOD and GOD is Love.<br /> <br />Anand Karia<br />Manager I.T<br />Artistic Garment Ind. Pvt. Ltd<br />MSC (Computer Science)
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
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
|
Java API Documentation
The Java Tutorial
|
 |
 |
|
|
subject: Passing Value from 1 class 2 another.
|
|
|