Results 1 to 14 of 14
- 08-23-2008, 12:29 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 29
- Rep Power
- 0
Making variables in a class accessible to all once changed
I have a class that just holds values that are updated as the program receives user input. But I want this classes variables once updated to accessible to all the other classes. I can't for the life of me remember how to do that.
class1 {
variable1;
variable2;
}
class2 {
class1 class = new class1();
class.variable1 = "Java";
}
class3 {
I want to be able to change the value of class.variable1 here, and have that value remain in variable1 and be accessible to all.
}
thanks in advance.
- 08-23-2008, 01:41 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
I think you need make variable1 as a class variable, so you'll need to use the static keyword to make variable1 a class variable.
Website: Learn Java by Examples
- 08-23-2008, 02:56 AM #3
Also if you make the variable public, other classes will be able to see/get it.
static doesn't make a variable a class variable. static means that the variable has only one existence no matter how many instances of the class there are. If there are multiple instances of a class, a static variable would be shared by them all. If any instance changed its value, all other instances would see the change.
A class variable would exist in each class, one per class.
Make a small test program and try the different combinations to see which solves your problem.Last edited by Norm; 08-23-2008 at 03:00 AM.
- 08-23-2008, 05:32 AM #4
Yea, when you declare like
public static int variable;
you can access it by "Class1.variable" from anywhere.
if you do it like...
public int variable ;
then you can access it by creating object of Class1 from any where.
Class1 cc=new Class1();
cc.variable=100;
- 08-23-2008, 06:13 AM #5
True, but doing so is a sin. Maybe two sins.
1) It exposes all the details to the world. You can not, for example, change it from an int to a float without breaking the world.
2) global variables radically increase coupling and cohesion. The point of OO is to get software reuse by decreasing coupling and cohesion.
- 08-23-2008, 06:39 AM #6
- 08-23-2008, 01:02 PM #7
Member
- Join Date
- Jul 2008
- Posts
- 29
- Rep Power
- 0
Thanks alot guys! Once you use static, you obviously don't have create new instances of it either, and they become more like C structs and inter-useable (if you know what I mean), just what I wanted!
It's all starting (slowly) to fall into place now.
Also, I have one last question:
If you are in a method within a method like this:
I want to know if it's possible to restart "MethodCall()" from within methodcallChecker(), so MethodCall starts again. It's been puzzling me for ages; I tried using "this", but that doesn't work.Java Code:class xxx { public void MethodCall(){ public void methodcallChecker(){ >> ///something is found to be true here, so restart methodcall() } } }
Again, thanks in advance.
-
does your "method within a method" code even compile? Please try that and get back to us with your result.
- 08-23-2008, 05:36 PM #9
Member
- Join Date
- Jul 2008
- Posts
- 29
- Rep Power
- 0
Ok, I get what you're saying...
I mean like that.Java Code:class xxx { public void MethodCall(){ class xxxx { public void methodcallChecker(){ >> ///something is found to be true here, so restart methodcall() } } } }
- 08-23-2008, 06:00 PM #10
He..He.. I belive....java does not allow this
- 08-23-2008, 06:54 PM #11
Not allow what?java does not allow this
The test would be to see if it compiles?
What does that mean? Would the Checker method throw an exception that would bubble up to the caller of the method MethodCall?so restart methodcall()
- 08-23-2008, 07:14 PM #12
Member
- Join Date
- Jul 2008
- Posts
- 29
- Rep Power
- 0
I'm pretty confused to tell you the truth.
MethodCall() is called from the main() class, so maybe I could call it in a for( ;; ) loop, and return back to the main class once methodcallChecker has found something to be true. Then it'll keep getting called anew?Java Code:class xxx { public void MethodCall(){ final class xxxx { public void methodcallChecker(){ >> ///something is found to be true here, so restart methodcall() } } } }
It's weird, I've actually managed to write a reporting engine for a MySQL database, but just get stuck on these weird little problems. I've got gaping holes in my Java knowledge that seriously hinder me. I really need to get reading.
- 08-23-2008, 08:06 PM #13
Java does not allow nested methods.
On the other hand method local inner classes are allowed
- 08-23-2008, 09:09 PM #14
Similar Threads
-
why my url string is changed in runtime ?
By aneuryzma in forum New To JavaReplies: 1Last Post: 08-13-2008, 09:46 PM -
Help with making this algorithm better
By RLRExtra in forum New To JavaReplies: 6Last Post: 01-17-2008, 04:11 PM -
Why the panel text changed?
By ottawalyli in forum AWT / SwingReplies: 1Last Post: 12-17-2007, 05:56 AM -
Why the panel text changed?
By ottawalyli in forum SWT / JFaceReplies: 0Last Post: 12-16-2007, 04:16 PM -
Strings are immutable yet they can be changed ?
By anjanesh in forum New To JavaReplies: 4Last Post: 05-19-2007, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks