|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-18-2008, 08:09 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
Static fields
Hi all,
All of you know about static fields(class variable). I just want to see your opinion on one thing, just make your comment here, not a question at all. 
public class MyClass {
static int myNumber = 0; // class variable
// do the rest of processing of MyClass
}
In the above class I've initialized a static field. I can refer that in two ways.
- Using the class name
- Using an object reference
Something like this.
MyClass.myNumber
myClassObject.myNumber
Actually even I courage to use the first way to do this, because the variable is a class level(I mean class variable) and easy to differ it from others.
But some people says that, the second way is the easiest because it is common to refer locals.
What is your comments on it.
Eanga
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-18-2008, 09:43 AM
|
 |
Member
|
|
Join Date: Apr 2008
Location: Chennai, India
Posts: 2
|
|
|
hi
hi,
my suggesstion reg. static members
A class have only one copy of Satic variables always.
whenever we change the class level static variable value (by using object reference or by using the classname) the changes reflect to all places whereever we use that variable. So whats the need of using object reference to access static variables.
Last edited by baskar.nitt : 04-18-2008 at 09:54 AM.
|
|

04-18-2008, 10:44 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
Ya I agreed with you. I'm just looking around how our community members think about it.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-18-2008, 12:24 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
|
|
Hello Eranga,
First think i would like to say that you started very good thread. Some times we should discuss things like that.
And in my opinion First One i.e. MyClass.myNumber is more fruitful because it gives the direct access to class members and methods. Calling method using object of class is popular but if we have Static methods or fields then we should call using ClassName.method instead of creating object and allocating memory for them.
sanjeev
|
|

04-18-2008, 12:53 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
Thanks Sanjeev,
You make a sense here for all of readers of this thread. Allocating memory. Even Java comes with automatic garbage collection, in real world application where hundreds of classes are involved, there can be a memory issue at all.
If you instantiation a object reference, each time memory is allocating.
Because of this reason also, I recommend to use the class name to refer static fields.
Any comments. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-18-2008, 02:54 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 256
|
|
If you instantiation a object reference, each time memory is allocating.
This should be avoided in serious applications.
I agree,
sukatoa
|
|

04-21-2008, 04:08 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
Originally Posted by sukatoa
This should be avoided in serious applications.
I agree,
sukatoa
Yep, this is one of the major issue we have work around in programming. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-21-2008, 09:14 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 88
|
|
|
Static Methods Are Only For Less Memory Usage?
Why we need to declare a method as static?
|
|

04-21-2008, 08:19 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 644
|
|
Memory allocation is definitely a good reason to avoid the second the way of doing it. I too prefer the first way dueo to allocation reasons and also due to more easier readability... knowing where the object is coming from... as I think someone said before.
Great points from all. 
__________________
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
|
|

04-22-2008, 04:43 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
Originally Posted by javarishi
Static Methods Are Only For Less Memory Usage?
Why we need to declare a method as static?
Because, static methods get there data as parameters. Then do some processing using those parameters, without not keeping a reference to variables.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-22-2008, 10:41 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 88
|
|
|
Eranga, I have one point. In abstract classes, if we make the methods as static we can invoke it without the instance, of the class right?
So can we use static methods in such kind of scenarios?
|
|

04-22-2008, 11:09 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
Actually abstract classes cannot be instantiated. But you can sub-class it.
Did you try with an example.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-22-2008, 11:14 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
Here is a simple example.
public abstract class AbstractTesting {
public static void main(String[] args) {
AbstractTesting.printMessage();
}
public static void printMessage() {
System.out.println("Java Forums!");
}
}
So, the answer to your question is, yes. But all it depends on the way you workout on the application, depend on the way you designed application/project. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-22-2008, 11:48 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 88
|
|
|
Yaah, Eranga. I asked some designers of our application. Their answer
was ,we generally put all utility methods as static. The reason which they
told was they are not the part of the behavior. So they are made as
static.
|
|

04-22-2008, 12:00 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
yes, basic idea is whenever working with real time applications ti should me as much as solid, safe, etc. So we have to take different actions to handle that.
Memory is one of that. Even Java has automatic garbage collection, it's not helped all the time.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-23-2008, 06:47 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Canada
Posts: 117
|
|
|
Hi Eranga,
I would always access the static methods through the class for clarity reasons. Clarity is always good :-). Static methods are in the end just global functions defined in the package/class namespace and with some access restrictions possibly thrown in.
|
|

04-29-2008, 10:00 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
I agreed with you Daniel. I always like to follows those kind of passions in programming. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-29-2008, 10:05 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 218
|
|
|
I never really work with static variables could you give me a time when it would be useful other than declaring final constants.
__________________
Definition of Impossible = making a good game in Java.
|
|

04-29-2008, 12:21 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
|
|
|
Have two meaning pal.
Static: Indicates that member can be called without the initial instantiating of a specific class.
Final: In Java define as an entity, that can't change in the future.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best? Vote Now
|
|

04-29-2008, 03:14 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Canada
Posts: 117
|
|
Zosden, a quick example that comes to mind is declaring the sole instance of a singleton as static 
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| |