Results 1 to 6 of 6
  1. #1
    sudharani is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Red face Static variables and methods

    in java a static method can access only static data members but main() is a static method
    still it is accessing the instance data members by creating an object of other class.
    how can it be possible?"

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sudharani View Post
    in java a static method can access only static data members but main() is a static method
    still it is accessing the instance data members by creating an object of other class.
    how can it be possible?"
    Show us an example; main( ... ) doesn't have special privileges; if it can create an object of another class and fiddle with its members those members are reachable for any other method (possibly in the same package).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It is creating objects that are local to the static method main. It's fine to do this, any method can create instances of objects locally for use in the method. Once the main method ends, the program is done.

    For instance, these two are fine
    Java Code:
    public class X{
      public static void XMethod(){
        String s = "Hello";
        System.out.println(s);
      }
      public void XMethod2(){
        String s = "Hello";
        System.out.println(s);
      }
    }
    It isn't referencing non static variables, it is creating it's on local object and using them.

  4. #4
    sudharani is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you.:)

  5. #5
    colorsjiya is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    static variables do not change between instances, meaning if you have two instances of class time.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sunde887 View Post
    It is creating objects that are local to the static method main. It's fine to do this, any method can create instances of objects locally for use in the method. Once the main method ends, the program is done.

    For instance, these two are fine
    Java Code:
    public class X{
      public static void XMethod(){
        String s = "Hello";
        System.out.println(s);
      }
      public void XMethod2(){
        String s = "Hello";
        System.out.println(s);
      }
    }
    It isn't referencing non static variables, it is creating it's on local object and using them.
    You and I must've answered to entirely differen questions; I thought that the question was about this (sort of):

    Java Code:
    public static void main(String[] args) {
       Whatever foo= new Whatever();
       foo.x= 42; // <-- main( ... ) is allowed to do this while others aren't ...
    }
    Oh well, as long as the OP is happy ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. cofusion with static and ordinary variables and methods
    By sandeepsai39 in forum New To Java
    Replies: 7
    Last Post: 09-02-2010, 12:25 PM
  2. Replies: 6
    Last Post: 02-15-2010, 04:31 AM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  4. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 PM
  5. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 09:52 AM

Tags for this Thread

Posting Permissions

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