Use of static methods


General J2EE: Use of static methods

  1. Use of static methods (1 messages)

    I know that you cannot call a non-static member from a static context in Java. For example, if a main() method makes a reference to a member variable within the same class then that variable must also be static.

    However, new classes can be instantiated from within a main method. If you create an instance of a new class within a main method must any variable or method that you call on that newly instantiated class also be static?
  2. Use of static methods[ Go to top ]

    A static member variable or method means it is associated with the CLASS and so every instance share same one ,non-static members is associated with a single object and so its value can vary among different object.

    Of course , you cannot directly invoke a non-static method or varaible within static context,because the compile can not decided which instance that invoked method or variale associated with. If you instansize a object ,you can use this object to invoke non-static methods.