Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Noob here forgive me(static question)

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Noob here forgive me(static question)

    I have a question about the key word static. Lets say for instance we have this piece of code.

    public class Foo
    {
    private int age;
    private static int weight;
    .. 
    ...
    }

    Say in main you create 2 objects. You change the age in one and then you change the weight in the other. Does that mean that the weight also changes in the first object as well? If that is the case then does that mean that weight is a pointer?

    I guess my question in a nutshell would be. How does static work? Is it essentially of a pointer type?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Noob here forgive me(static question)

    Quote Originally Posted by camel-man View Post
    Does that mean that the weight also changes in the first object as well? If that is the case then does that mean that weight is a pointer?
    What happened when you tried it?

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Noob here forgive me(static question)

    camel-man,
    Do some study on static and instance variables in java. You can refer to the textbook "Java The Complete Reference". It has nice description about static keyword.

    Syed.

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Noob here forgive me(static question)

    JPS I don't mean to step on your toes, but this is actually an interesting question. And, it is nice to have you here to grab the wheel if I veer off. To my understanding java uses stacks and heaps. When you create weight it is a strong reference to the root, however age is only a reference to the object foo. Essentially yes both names (I.E: age and weight) are references (pointers), the difference between them is their respective dumpability (made up word). Weight is only dumpable when the initial root is gone, however as soon as the corresponding foo object has lost its relevance to the root then age has and is consequently dumpable.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Noob here forgive me(static question)

    @KAJLogic stack/heap memory is irreverent here.

    I would take syed's advice and read more about what static does. See: Understanding Instance and Class members.

  6. #6
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Noob here forgive me(static question)

    Actually if I can be candid it seems your response is the irrelevant one.
    His question was;
    "I guess my question in a nutshell would be. How does static work? Is it essentially of a pointer type?"

    He already knew what would happen; he was now wondering why. My answer was specific to his question. He did need to know hum drum information and examples.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Noob here forgive me(static question)

    It's irrelevant because it doesn't matter if weight/age are pointers/references or not. They shouldn't be, but Java's memory model is all screwy and I'd have to look at a the JVM specs in detail to tell if they are or not.

    The thing is that weight is a member of the class foo, where-as age is a member of an instance of foo.

    Static access works against class foo -> weight, and because every accessor references the same foo class, they operate on the same weight (doesn't matter if it is a reference or not, neither would be in C++). Only one instance of the class foo is allowed per JVM.

    On the other hand, age belongs to a specific instance of a foo object. So accessing age would go through foo object 1 -> age, or foo object 2 -> age. Again, if you reference the same foo object you will be using the same age. Referencing different foo objects means your accessing different ages.

    There is no "weight" of a foo object. It belongs to the foo class. Accessing foo object 1 -> weight is implicitly translated to class foo -> weight.

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Noob here forgive me(static question)

    Quote Originally Posted by KAJLogic View Post
    however age is only a reference to the object foo.
    age is a member of a foo object, you can get to age through an instance of foo, but you can not have age without an instance of foo, and there is no way to get from age back to foo, only from foo to age. (foo.age but age.foo doesn't work, so age can not refer to foo in any way)

    Quote Originally Posted by KAJLogic View Post
    Essentially yes both names (I.E: age and weight) are references (pointers)
    We try not to use the word pointer in Java because of the confusion among languages which support real pointers.

    Quote Originally Posted by KAJLogic View Post
    He already knew what would happen; he was now wondering why.
    That is not how it works here (mostly). First you do your research, you try it out and see what happens. If you can not figure out the way it works with the combined effort of research and experimentation you post a sample code, explain what happens/does not happen, and ask any questions you have. We are not here to do the dirty work, but to give a push in the right direction.

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Noob here forgive me(static question)

    Quote Originally Posted by helloworld922 View Post
    It's irrelevant because it doesn't matter if weight/age are pointers/references or not. They shouldn't be, but Java's memory model is all screwy and I'd have to look at a the JVM specs in detail to tell if they are or not.

    The thing is that weight is a member of the class foo, where-as age is a member of an instance of foo.

    Static access works against class foo -> weight, and because every accessor references the same foo class, they operate on the same weight (doesn't matter if it is a reference or not, neither would be in C++). Only one instance of the class foo is allowed per JVM.

    On the other hand, age belongs to a specific instance of a foo object. So accessing age would go through foo object 1 -> age, or foo object 2 -> age. Again, if you reference the same foo object you will be using the same age. Referencing different foo objects means your accessing different ages.

    There is no "weight" of a foo object. It belongs to the foo class. Accessing foo object 1 -> weight is implicitly translated to class foo -> weight.
    Perhaps I'm projecting, but if he wanted the most basic description of static v.s non-static variables he would do a very simple google search. I know from experience it can be tasking when you ask a specific question like WHY, and you get a completely different answer.

    TO JPS:

    when I said age is a reference to foo I meant on the stack foo references age in memory, there-by age is in reference to the stack variable foo; I should probably be careful about my word usage in the last part I was using reference the word not the term.

    Finally in conclusion:

    Now from what I can suss out from your rant hello either you think your enlightening me to the world of static and non-static variables, or you are just gravely mistaken on how the stack/heap work. Oh, and by-the-by Java's memory model is prime, and more advanced than the alternative. Automatic dumping was only a logical end point. If you want you can specify with the right amount of source editing anyway.

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Noob here forgive me(static question)

    I am trying to clarify the OP's original remark:

    Does that mean that the weight also changes in the first object as well?
    This is an incorrect assumption of how static works. The simplest way to think of static is that it belongs to the class, not an instance of the class. Trying to access a static member of a class using an instance is technically allowed, but it should be viewed as a shortcut notation for accessing the static member through the class. An actual instance should not have extra hidden reference members pointing to the static class members.

    my_instance = new MyClass();
    my_instance.static_field = 3;
    // should really read this as:
    MyClass.static_field = 3;

    Both age and weight theoretically could belong on the stack, but I suspect they don't because all Java instances are heap-allocated, and I suspect classes are also heap-allocated because they can be dynamically loaded. There might be some optimizations going on behind the scene which may result in the variables actually being allocated on the stack, but there's no guarantee without digging through the JVM specs or an actual implementation. Static has little/no bearing on whether memory is placed on the stack or heap, and likewise thinking of either as analogous to a C++ style reference or pointer can lead to some incorrect assumptions.

    In any case, this seems beyond the scope of the OP's question. Their original question was more along the lines of "What does static do?" rather than "What are the implementation details behind static?". I'm trying to answer the former, you're trying to answer the latter.

Similar Threads

  1. very noob question
    By javapol in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2013, 03:24 AM
  2. Noob Question
    By javapol in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 23rd, 2013, 08:24 AM
  3. Most noob question ever.
    By SkyAphid in forum Java Theory & Questions
    Replies: 1
    Last Post: December 8th, 2011, 08:14 AM
  4. Total NOOB question
    By coolidge in forum Java Theory & Questions
    Replies: 3
    Last Post: September 2nd, 2011, 04:09 PM
  5. Noob question: Why .class?
    By Shaybay92 in forum Java Theory & Questions
    Replies: 10
    Last Post: August 22nd, 2011, 04:56 AM