Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-18-2008, 08:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
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.
  1. Using the class name
  2. Using an object reference

Something like this.

Code:
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-18-2008, 09:43 AM
baskar.nitt's Avatar
Member
 
Join Date: Apr 2008
Location: Chennai, India
Posts: 2
baskar.nitt is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-18-2008, 10:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-18-2008, 12:24 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Mumbai(India)
Posts: 230
sanjeevtarar is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-18-2008, 12:53 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-18-2008, 02:54 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 256
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
If you instantiation a object reference, each time memory is allocating.
This should be avoided in serious applications.

I agree,
sukatoa
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-21-2008, 04:08 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by sukatoa View Post
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-21-2008, 09:14 AM
Member
 
Join Date: Apr 2008
Posts: 88
javarishi is on a distinguished road
Static Methods Are Only For Less Memory Usage?

Why we need to declare a method as static?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-21-2008, 08:19 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 644
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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)
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-22-2008, 04:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by javarishi View Post
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-22-2008, 10:41 AM
Member
 
Join Date: Apr 2008
Posts: 88
javarishi is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-22-2008, 11:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-22-2008, 11:14 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Here is a simple example.

Code:
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
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-22-2008, 11:48 AM
Member
 
Join Date: Apr 2008
Posts: 88
javarishi is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-22-2008, 12:00 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-23-2008, 06:47 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 117
danielstoner is on a distinguished road
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.
__________________
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 04-29-2008, 10:00 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 04-29-2008, 10:05 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 04-29-2008, 12:21 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,168
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 04-29-2008, 03:14 PM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 117
danielstoner is on a distinguished road
Zosden, a quick example that comes to mind is declaring the sole instance of a singleton as static
__________________
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter