Results 1 to 9 of 9
  1. #1
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default about static thing

    I just wonder why about the 4th constraint for field/method with static modifier. Why can't class methods access instance variables or instance method directly?
    I noticed it from Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Not all combinations of instance and class variables and methods are allowed:
    1.Instance methods can access instance variables and instance methods directly.
    2.Instance methods can access class variables and class methods directly.
    3.Class methods can access class variables and class methods directly.
    4.Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    Because instance (non-static) variables and methods belong to a particular instance of a class. Class (static) variables and methods do not belong to a particular instance of a class. If a static method tried to access a non-static variable, which instance's variable should it be accessing? Keep in mind, that 10 different instances can have 10 different values for any of the non-static variables.

    If you don't understand what I'm talking about, throw together an example SSCCE and we'll go from there.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    turk is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    When you create an object, you create an instance of this object. This instance is the only one that can access the non-static public methods of this object. If you access a non-static method from an object that doesn't exist, the program will crash since non of its variables and non-static methods had been created.

    Imagine an object Person. When you create a Person you may set its name and last name. This object is the only one that can access its public instance variables and its public instance methods (set name, get name, set last name, get last name). Nobody else but this Person knows about its name or last name. If someone else needs this information, it is needed to be retrieved through the object Person like person.name();

    When you declare a method public and static it means that you don't need to create an object before using it. Like Math.pow(x, y) it will be used for general purposes and can be called without creating an object. Although in this static methods you can't use anything that is non-static since you didn't create any object.


    Does this make sense?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    I'm not trying to be pedantic, but some of what you said might be misleading:

    Quote Originally Posted by turk View Post
    When you create an object, you create an instance of this object. This instance is the only one that can access the non-static public methods of this object.
    That's not true. Anything that has the instance in scope can access the public methods of that Object.


    Quote Originally Posted by turk View Post
    If you access a non-static method from an object that doesn't exist, the program will crash since non of its variables and non-static methods had been created.
    I'm actually pretty sure this would be a compile-time error, not a program crash.

    Quote Originally Posted by turk View Post
    Imagine an object Person. When you create a Person you may set its name and last name. This object is the only one that can access its public instance variables and its public instance methods (set name, get name, set last name, get last name). Nobody else but this Person knows about its name or last name. If someone else needs this information, it is needed to be retrieved through the object Person like person.name();
    Again, if the instance variables (or methods) are public, then they can be accessed by any caller that has the instance in scope.

    Quote Originally Posted by turk View Post
    When you declare a method public and static it means that you don't need to create an object before using it. Like Math.pow(x, y) it will be used for general purposes and can be called without creating an object. Although in this static methods you can't use anything that is non-static since you didn't create any object.
    Or, you created many Objects and have no way of knowing which one to use. But yeah, if you're in a static context, it doesn't make sense to access non-static variables or methods.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    public class X{
    public static void accessY(){
    System.out.println(Y.y);
    }
    }
    
    public class Y{
    public int y;
    public y(){
    this.y = 10;
    }
    }
    how about this code?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ryozkidz View Post
    Java Code:
    public class X{
    public static void accessY(){
    System.out.println(Y.y);
    }
    }
    
    public class Y{
    public int y;
    public y(){
    this.y = 10;
    }
    }
    how about this code?
    What did your friendly compiler say about it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    compilation error..~ hahaha....
    if i can't access instance variable from static method, what if i have to access it badly, what should i do?

  8. #8
    ryozkidz is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    can i pass the object Y as an argument into the method?

    public static void accessY( Y yyy )
    {
    System.out.println(yyy.y);
    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ryozkidz View Post
    can i pass the object Y as an argument into the method?

    public static void accessY( Y yyy )
    {
    System.out.println(yyy.y);
    }
    Sure you can; non-static methods are like messages you want to pass to something. If there is no 'something' you can't pass the message. If you have a 'something' named yyy you can pass messages that something can understand.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  2. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  3. Replies: 4
    Last Post: 05-12-2010, 11:05 AM
  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
  •