Can A Not Static Method Access A Static Variable/Method Inwards Java?
Sunday, March 11, 2018
Add Comment
"Can a non-static method access a static variable or telephone telephone a static method" is i of the often asked questions on static modifier inward Java, the reply is, Yes, a non-static method tin access a static variable or telephone telephone a static method inward Java. There is no employment amongst that because of static members i.e. both static variable together with static methods belongs to a aeroplane together with tin move called from anywhere, depending upon their access modifier. For example, if a static variable is private together with then it tin exclusively move accessed from the aeroplane itself, but you lot tin access a world static variable from anywhere. Similarly, a somebody static method tin move called from a non-static method of the same aeroplane but a world static method e.g. main() tin move called from anywhere.
Here is a code instance to seek our betoken that a non-static method tin access both static variable together with method inward Java:
You tin run across that this code compiles simply fine, at that spot is no compile fourth dimension error. You tin fifty-fifty access a nested static class from a non-static method, it absolutely fine.
But, simply think, If the reply is that simple, together with then why this enquiry is often asked on Java interviews together with Java certifications similar OCAJP or OCPJP? Well, the enquiry is a piddling chip tricky together with often asked confused candidates because the opposite is non true i.e. you lot tin access static members from non-static context but you cannot access a non-static variable or method from a static method inward Java.
Why you lot cannot access a non-static variable or telephone telephone a non-static method from a static method inward Java? Well, this is because a static method forms a static context where exclusively static members tin move accessed, but if you lot desire to a greater extent than explanation, I propose you lot acquire through i of the to a greater extent than comprehensive resources similar non-static fellow member variables or methods cannot move accessed from a static method inward Java:
$ javac Hello.java
Hello.java:11: non-static variable aNonStaticVariable cannot move referenced from a static context
System.out.println(aNonStaticVariable);
^
Hello.java:12: non-static method aNonStaticMethod() cannot move referenced from a static context
aNonStaticMethod();
^
2 errors
You tin run across that fifty-fifty though you lot tin access static members from a non-static method, the reverse is non true. If you lot endeavor to access a non-static variable or method or fifty-fifty a nested class, the compiler volition throw fault "non-static method XXXX cannot move referenced from a static context".
So, straightaway the big enquiry comes how tin you lot access a non-static variable or telephone telephone a non-static method from a static method e.g. main() method inward Java? let's uncovering out.
$ javac Hello.java
You tin run across that all compile-time fault has gone subsequently access non-static variable together with method using an object of the Hello class. This is the correct means to access a non-static variables/methods from a static context e.g. a static initializer block, static method, or a nested static aeroplane inward Java. See The Complete Java Masterclass for to a greater extent than details.
That's all most whether a non-static method tin access static variable or method inward Java or not. Of course, they tin but the reverse is non truthful i.e. you lot cannot access a non-static fellow member from a static context i.e. static method. The exclusively means to access a non-static variable from a static method is yesteryear creating an object of the aeroplane the variable belongs.
This confusion is the principal argue why you lot run across this enquiry on heart together with soul Java interview every bit good every bit on heart together with soul Java certifications e.g. OCAJP together with OCPJP exam. You volition uncovering a lot of questions based on static concepts on OCAJP, especially on Whizlabs Java 8 Exam Simulator hence it is really of import to laid upwardly this theme well.
Further Learning
answer)Can you lot overload or override static method inward Java? (answer) Can nosotros declare a aeroplane static inward Java? (answer) Can you lot brand an array volatile inward Java? (answer) Can you lot run a Java computer program without main() method inward Java? (answer) Can you lot brand an abstract aeroplane terminal inward Java? (answer) Can you lot override a somebody method inward Java? (answer) Top v Free Java 8 together with Java nine courses for Programmers (courses)
Thanks for reading this article, if you lot similar this article together with then delight portion amongst your friends together with colleagues. If you lot accept whatsoever questions or feedback together with then delight drib a comment.
P. S. - If you lot are preparing for Java certification together with desire to laid upwardly this theme good than reading a practiced heart together with soul Java mass like OCAJP Study Guide yesteryear Mala Gupta can too assistance a lot. This is an splendid mass to larn heart together with soul Java fundamentals fifty-fifty if you lot are non preparing for exams.
Sumber https://javarevisited.blogspot.com/
Here is a code instance to seek our betoken that a non-static method tin access both static variable together with method inward Java:
public class StaticTest { public static int iStatic = 10; public void nonStatic() { System.out.println("can access static variable within non-static method : " + iStatic); main(new String[2]); } public static void main(String[] args) { System.out.println("Inside principal method"); } }
You tin run across that this code compiles simply fine, at that spot is no compile fourth dimension error. You tin fifty-fifty access a nested static class from a non-static method, it absolutely fine.
But, simply think, If the reply is that simple, together with then why this enquiry is often asked on Java interviews together with Java certifications similar OCAJP or OCPJP? Well, the enquiry is a piddling chip tricky together with often asked confused candidates because the opposite is non true i.e. you lot tin access static members from non-static context but you cannot access a non-static variable or method from a static method inward Java.
Why you lot cannot access a non-static variable or telephone telephone a non-static method from a static method inward Java? Well, this is because a static method forms a static context where exclusively static members tin move accessed, but if you lot desire to a greater extent than explanation, I propose you lot acquire through i of the to a greater extent than comprehensive resources similar non-static fellow member variables or methods cannot move accessed from a static method inward Java:
class Hello { private static int aStaticVariable = 1; private int aNonStaticVariable = 2; private static void aStaticMethod() { System.out.println(aNonStaticVariable); aNonStaticMethod(); } private void aNonStaticMethod() { System.out.println(aStaticVariable); } }
Hello.java:11: non-static variable aNonStaticVariable cannot move referenced from a static context
System.out.println(aNonStaticVariable);
^
Hello.java:12: non-static method aNonStaticMethod() cannot move referenced from a static context
aNonStaticMethod();
^
2 errors
You tin run across that fifty-fifty though you lot tin access static members from a non-static method, the reverse is non true. If you lot endeavor to access a non-static variable or method or fifty-fifty a nested class, the compiler volition throw fault "non-static method XXXX cannot move referenced from a static context".
So, straightaway the big enquiry comes how tin you lot access a non-static variable or telephone telephone a non-static method from a static method e.g. main() method inward Java? let's uncovering out.
How to access a non-static variable/method from a static method inward Java
Well, at that spot is a legitimate means to access whatsoever non-static fellow member from the static context inward Java, yesteryear creating instances. You demand to showtime do an object of the aeroplane whose non-static members or non-static method you lot desire to access. Once you lot do that, the compiler volition non bother you lot anymore every bit shown inward the next example:public class Hello { private static int aStaticVariable = 1; private int aNonStaticVariable = 2; private static void aStaticMethod() { Hello object = new Hello(); System.out.println(object.aNonStaticVariable); object.aNonStaticMethod(); } private void aNonStaticMethod() { System.out.println(aStaticVariable); } }
$ javac Hello.java
You tin run across that all compile-time fault has gone subsequently access non-static variable together with method using an object of the Hello class. This is the correct means to access a non-static variables/methods from a static context e.g. a static initializer block, static method, or a nested static aeroplane inward Java. See The Complete Java Masterclass for to a greater extent than details.
That's all most whether a non-static method tin access static variable or method inward Java or not. Of course, they tin but the reverse is non truthful i.e. you lot cannot access a non-static fellow member from a static context i.e. static method. The exclusively means to access a non-static variable from a static method is yesteryear creating an object of the aeroplane the variable belongs.
This confusion is the principal argue why you lot run across this enquiry on heart together with soul Java interview every bit good every bit on heart together with soul Java certifications e.g. OCAJP together with OCPJP exam. You volition uncovering a lot of questions based on static concepts on OCAJP, especially on Whizlabs Java 8 Exam Simulator hence it is really of import to laid upwardly this theme well.
Further Learning
answer)
Thanks for reading this article, if you lot similar this article together with then delight portion amongst your friends together with colleagues. If you lot accept whatsoever questions or feedback together with then delight drib a comment.
P. S. - If you lot are preparing for Java certification together with desire to laid upwardly this theme good than reading a practiced heart together with soul Java mass like OCAJP Study Guide yesteryear Mala Gupta can too assistance a lot. This is an splendid mass to larn heart together with soul Java fundamentals fifty-fifty if you lot are non preparing for exams.
0 Response to "Can A Not Static Method Access A Static Variable/Method Inwards Java?"
Post a Comment