answersLogoWhite

0


Best Answer

In java we access static variables and static methods without creating objects.

i.e.,we can access directly by using classname

we can also access static variables and static methods by using objects which are created by using class where the static variables and static methods are available

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you access the static variable and static methods of a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

It is perfectly legal to any instance variable inside of a static method?

No. You will get compilation errors. The complier will complain that you are trying to access non static variables from inside a static method. A static method can access only static variables.


Can a static method access instance variables?

Variables cannot access variables; only methods can access variables. Non-static methods (also known as instance methods) are local to an object of the class and therefore have access to a "this" reference (referring to the current instance of the class, the object upon which the method was invoked), but static variables are local to the class itself. These variables are shared by all objects of the class and are therefore accessible to non-static methods. Static variable are also accessible to static methods and are therefore accessible even when no objects of the class exist.


Static method can use only static variable why?

A static method is a method that is a class method and is not attached to the object of that class. So if we use a non static variable of the class, it would most probably not have been initialized because no object could have been created for the class. Hence it would throw a null pointer exception. To avoid such an ambiguity, there is a restriction that static methods can use only static variables. This is to ensure that class methods can access only class variables both of which would get initialized simultaneously.


Is it possible to call static methods with out creating object?

Yes, we can access all methods which declare with static means then we can access.. ex: class s{ static method() { System.out.println("Welcome"); } } class fun{ public static void main(String args[]) { method(); } }


Can a method be static and synchronized?

Yes. In Java methods can be static and synchronized. Static methods access other static members in the class. Static in case of inheritance are treated as non - static. Synchronized methods are those which have dedicated thread attached to it and no other can access until currrent thread leaves the control from it.


What is the static variable in the class in c?

A static member variable is local to the class rather than to an object of the class.


Why a static member method can access only static members in java?

Because, the keyword static signifies the fact that the method or variable that is qualified using the static keyword is not attached to any object of the class. Therefore we cannot instantiate the class and use the object to reference to access it. The only option we have is to use the class name to directly access them


How do you declare a member of a class static?

Static MethodsStatic keyword when used with a method, specifies that this method belongs to the class and not a particular instance of the class (a.k.a object of the class)Ex:public class StaticTest {public static String getAuthorName() {return "Anand";}}Here getAuthorName is the static method and it can be accessed without instantiating an object of the class StaticTest. You can access this method as:String authorName = StaticTest.getAuthorName();Tip: It is important to know that, a static method cannot and I mean CANNOT access instance variables or methods of the class. It can only access other static methods and variables.For Ex:public class StaticTestBad {private String name = "Rocky";public static String getAuthorName () {return name; //Cant work!!!}public int getAge(){return 28;}public static int getAuthorAge() {return getAge(); //Cant Work Again!!!}}If you try to compile this class, you will get compilation errors at the two lines that I have commented out.Static VariablesThe static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.


What are Instance variables java?

Instance variableA variable, part of an Object. These might better be called perObject variables since each instantiated object of this class will have its own private copy of this variable. They are allocated when the object is allocated via new. Static methods may not access the instance variables of their class (or any other class for that matter), other that via some object reference, e.g. anObject.someField. Static methods may even access private instance variables in their class via some object reference.


Why is it not advisable to use this pointer with static members?

It is not inadvisable, it is impossible. Static member methods do not have access to a this pointer since they are not associated with any instance. Static members are scoped to the class, not to an object (an instance of the class). Only instance members have access to the this pointer.


Advantages of static keyword in java?

When we want that a class member should be used independently of any object of that class, we have to precede that member's declaration with the keyword static .When a member is declared as static, it can be acccessed without creationg an object of the class and without referring to the object.That static is , a static member belons to a class as a whole and not to any one instance/object of that class.The static member is shared by all objects of thatclass.


Java Which access modifier is preferred for instance variables?

Usually Private is the preferred access modifier for instance variables. Benefits: 1. No other class can access this variable directly. They can do only through the getter/setter methods 2. Only the methods in that particular class can use this variable