Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: Static fields

  1. #1
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Wink 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. :p

    Java 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.

    Java 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

  2. #2
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default 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 08:54 AM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. :)

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    If you instantiation a object reference, each time memory is allocating.
    This should be avoided in serious applications.

    I agree,
    sukatoa

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. :)

  8. #8
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Static Methods Are Only For Less Memory Usage?

    Why we need to declare a method as static?

  9. #9
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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. ;)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  11. #11
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    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?

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually abstract classes cannot be instantiated. But you can sub-class it.

    Did you try with an example.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is a simple example.

    Java 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. :)

  14. #14
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    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.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  16. #16
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  18. #18
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I never really work with static variables could you give me a time when it would be useful other than declaring final constants.
    My IP address is 127.0.0.1

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  20. #20
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 12:35 AM
  2. Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  3. Jsp / Java cannot get fields
    By Dracos in forum New To Java
    Replies: 0
    Last Post: 02-04-2008, 09:39 PM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •