Can A Not Static Method Access A Static Variable/Method Inwards Java?
"Can a non-static method access a static variable or telephone yell upward a static method" is 1 of the oft asked questions on static modifier inward Java, the reply is, Yes, a non-static method tin access a static variable or telephone yell upward a static method inward Java. There is no work alongside that because of static members i.e. both static variable together with static methods belongs to a class together with tin live called from anywhere, depending upon their access modifier. For example, if a static variable is private together with so it tin exclusively live accessed from the class itself, but y'all tin access a populace static variable from anywhere. Similarly, a mortal static method tin live called from a non-static method of the same class but a populace static method e.g. main() tin live called from anywhere.
Here is a code illustration to attempt our signal that a non-static method tin access both static variable together with method inward Java:
You tin come across that this code compiles only fine, in that place is no compile fourth dimension error. You tin fifty-fifty access a nested static class from a non-static method, it absolutely fine.
But, only think, If the reply is that simple, together with so why this interrogation is oft asked on Java interviews together with Java certifications similar OCAJP or OCPJP? Well, the interrogation is a niggling flake tricky together with often asked confused candidates because the opposite is non true i.e. y'all 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 y'all cannot access a non-static variable or telephone yell upward 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 live accessed, but if y'all desire to a greater extent than explanation, I advise y'all become through 1 of the to a greater extent than comprehensive resources similar non-static fellow member variables or methods cannot live accessed from a static method inward Java:
$ javac Hello.java
Hello.java:11: non-static variable aNonStaticVariable cannot live referenced from a static context
System.out.println(aNonStaticVariable);
^
Hello.java:12: non-static method aNonStaticMethod() cannot live referenced from a static context
aNonStaticMethod();
^
2 errors
You tin come across that fifty-fifty though y'all tin access static members from a non-static method, the contrary is non true. If y'all endeavour to access a non-static variable or method or fifty-fifty a nested class, the compiler volition throw mistake "non-static method XXXX cannot live referenced from a static context".
So, forthwith the big interrogation comes how tin y'all access a non-static variable or telephone yell upward a non-static method from a static method e.g. main() method inward Java? let's detect out.
$ javac Hello.java
You tin come across that all compile-time mistake has gone afterward access non-static variable together with method using an object of the Hello class. This is the correct agency to access a non-static variables/methods from a static context e.g. a static initializer block, static method, or a nested static class inward Java. See The Complete Java Masterclass for to a greater extent than details.
That's all virtually whether a non-static method tin access static variable or method inward Java or not. Of course, they tin but the contrary is non truthful i.e. y'all cannot access a non-static fellow member from a static context i.e. static method. The exclusively agency to access a non-static variable from a static method is yesteryear creating an object of the class the variable belongs.
This confusion is the primary argue why y'all come across this interrogation on meat Java interview equally good equally on meat Java certifications e.g. OCAJP together with OCPJP exam. You volition detect a lot of questions based on static concepts on OCAJP, especially on Whizlabs Java 8 Exam Simulator hence it is really of import to gear upward this theme well.
Further Learning
answer)Can y'all overload or override static method inward Java? (answer) Can nosotros declare a class static inward Java? (answer) Can y'all brand an array volatile inward Java? (answer) Can y'all run a Java programme without main() method inward Java? (answer) Can y'all brand an abstract class lastly inward Java? (answer) Can y'all override a mortal method inward Java? (answer) Top v Free Java 8 together with Java nine courses for Programmers (courses)
Thanks for reading this article, if y'all similar this article together with so delight part alongside your friends together with colleagues. If y'all accept whatever questions or feedback together with so delight driblet a comment.
P. S. - If y'all are preparing for Java certification together with desire to gear upward this theme good than reading a expert meat Java mass like OCAJP Study Guide yesteryear Mala Gupta can equally good aid a lot. This is an first-class mass to larn meat Java fundamentals fifty-fifty if y'all are non preparing for exams.
Here is a code illustration to attempt our signal 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 primary method"); } }
You tin come across that this code compiles only fine, in that place is no compile fourth dimension error. You tin fifty-fifty access a nested static class from a non-static method, it absolutely fine.
But, only think, If the reply is that simple, together with so why this interrogation is oft asked on Java interviews together with Java certifications similar OCAJP or OCPJP? Well, the interrogation is a niggling flake tricky together with often asked confused candidates because the opposite is non true i.e. y'all 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 y'all cannot access a non-static variable or telephone yell upward 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 live accessed, but if y'all desire to a greater extent than explanation, I advise y'all become through 1 of the to a greater extent than comprehensive resources similar non-static fellow member variables or methods cannot live 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 live referenced from a static context
System.out.println(aNonStaticVariable);
^
Hello.java:12: non-static method aNonStaticMethod() cannot live referenced from a static context
aNonStaticMethod();
^
2 errors
You tin come across that fifty-fifty though y'all tin access static members from a non-static method, the contrary is non true. If y'all endeavour to access a non-static variable or method or fifty-fifty a nested class, the compiler volition throw mistake "non-static method XXXX cannot live referenced from a static context".
So, forthwith the big interrogation comes how tin y'all access a non-static variable or telephone yell upward a non-static method from a static method e.g. main() method inward Java? let's detect out.
How to access a non-static variable/method from a static method inward Java
Well, in that place is a legitimate agency to access whatever non-static fellow member from the static context inward Java, yesteryear creating instances. You demand to showtime practise an object of the class whose non-static members or non-static method y'all desire to access. Once y'all practise that, the compiler volition non bother y'all anymore equally 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 come across that all compile-time mistake has gone afterward access non-static variable together with method using an object of the Hello class. This is the correct agency to access a non-static variables/methods from a static context e.g. a static initializer block, static method, or a nested static class inward Java. See The Complete Java Masterclass for to a greater extent than details.
That's all virtually whether a non-static method tin access static variable or method inward Java or not. Of course, they tin but the contrary is non truthful i.e. y'all cannot access a non-static fellow member from a static context i.e. static method. The exclusively agency to access a non-static variable from a static method is yesteryear creating an object of the class the variable belongs.
This confusion is the primary argue why y'all come across this interrogation on meat Java interview equally good equally on meat Java certifications e.g. OCAJP together with OCPJP exam. You volition detect a lot of questions based on static concepts on OCAJP, especially on Whizlabs Java 8 Exam Simulator hence it is really of import to gear upward this theme well.
Further Learning
answer)
Thanks for reading this article, if y'all similar this article together with so delight part alongside your friends together with colleagues. If y'all accept whatever questions or feedback together with so delight driblet a comment.
P. S. - If y'all are preparing for Java certification together with desire to gear upward this theme good than reading a expert meat Java mass like OCAJP Study Guide yesteryear Mala Gupta can equally good aid a lot. This is an first-class mass to larn meat Java fundamentals fifty-fifty if y'all are non preparing for exams.
Belum ada Komentar untuk "Can A Not Static Method Access A Static Variable/Method Inwards Java?"
Posting Komentar