Two Laptop Bag*
The moose likes Beginning Java and the fly likes (static ) variables in multple classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "(static ) variables in multple classes" Watch "(static ) variables in multple classes" New topic
Author

(static ) variables in multple classes

Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Hi
i developed a program that getting longer as far as i develop it so i divide it to 5 logical classes.
i find out that each class of the 5 classes need to use a variables that was declared in another class, therefor i used a static modifier to declare some multi-using variables.since i have a lots of variables i'll ask you those:
1. there is a form that let me to use those variable in all the classes without creating a new instance of each class and without declare all the variales static?
2. i know that the meaning of static is to make a class variables instead of instance variables therefor i wonder what the use of static in declaring a class( public static class myClass)??
thanks
Shay
[This message has been edited by Shay Gaghe (edited September 15, 2001).]
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Originally posted by Shay Gaghe:

1. there is a form that let me to use those variable in all the classes without creating a new instance of each class and without declare all the variales static?


Hi
You could put them into an interface. Any variable you declare in an interface is by default public static and final. Ensure this interface is in scope for all your other classes, then when you want to use one of these 'global'(use the term loosely) variables, you can do so simply by using the fully qualified name like:
<code>TheInterfaceName.THE_CONSTANT_NAME</code>.
They would, however, still be static. Why do you want to avoid this?
Originally posted by Shay Gaghe:

2. i know that the meaning of static is to make a class variables instead of instance variables therefor i wonder what the use of static in declaring a class( public static class myClass)??


You cannot declare a class as above, unless its an inner class. If it is an inner class, then that inner class belongs to the enclosing CLASS, rather than the enclosing INSTANCE.
Hope this helps
Michael
------------------
"One good thing about music - when it hits, you feel no pain"
Bob Marley


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Originally posted by Michael Fitzmaurice:
You cannot declare a class as above, unless its an inner class. If it is an inner class, then that inner class belongs to the enclosing CLASS, rather than the enclosing INSTANCE.
Hope this helps
Michael

Hi Micheal!
USAID : Ensure this interface is in scope for all your other classes,
I know that whenever I need to use interface I have to use implement modifier within the class decleration. Should I implement this interface in all of the classes(sorry about the questions , it�s the first time Im going to use interface)?
USAID : Any variable you declare in an interface is by default public static and final
I need my variables not to be final , what the rules of doing that?
USAID :
Why do you want to avoid this?
First I find it more understandable if I type my className.memberName , second I think that its more efficiant to use the method directly instead of creating instance of the class , I might be wrong�
I�ll try this form
Thank a lot about your reply
Shay Gaghe
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Yes, you have to implement the interface(s) in all your classes if you will be using them there. if you have a class that estends a class that already implements the interface(s), then you don't need to implement it.
If you don't want you variables to be static and final, then don't put them in the interface.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Originally posted by Bosun Bello:
Yes, you have to implement the interface(s) in all your classes if you will be using them there. if you have a class that estends a class that already implements the interface(s), then you don't need to implement it.
If you don't want you variables to be static and final, then don't put them in the interface.
Bosun

thanks for the reply
like i said i need my variabkes to be static to lett all mt classes use them but i need then just static not final
how can i disable the final behaveior?

Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You can declare your variables as static in a superclass. And just extend that class from your other classes. Of course, you can only extend one class in Java.

Bosun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: (static ) variables in multple classes
 
Similar Threads
Jxam
static and non-static relationship question
Collaboration between separate parts(classes) in the same framework
inner class using forward reference doubt
About multiple extending classes