Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Passing Value from 1 class 2 another. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Value from 1 class 2 another." Watch "Passing Value from 1 class 2 another." New topic

Passing Value from 1 class 2 another.

Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

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?


Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Passing Value from 1 class 2 another.
It's not a secret anymore!