Can A Not Static Method Access A Static Variable/Method Inward Java?

"Can a non-static method access a static variable or telephone weep upwardly a static method" is ane of the ofttimes asked questions on static modifier inwards Java, the respond is, Yes, a non-static method tin laissez passer on notice access a static variable or telephone weep upwardly a static method inwards Java. There is no work amongst that because of static members i.e. both static variable too static methods belongs to a shape too tin laissez passer on notice survive called from anywhere, depending upon their access modifier. For example, if a static variable is private so it tin laissez passer on notice alone survive accessed from the shape itself, but you lot tin laissez passer on notice access a world static variable from anywhere. Similarly, a mortal static method tin laissez passer on notice survive called from a non-static method of the same shape but a world static method e.g. main() tin laissez passer on notice survive called from anywhere.

Here is a code instance to test our request that a non-static method tin laissez passer on notice access both static variable too method inwards 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 master copy method");   }  }

You tin laissez passer on notice run across that this code compiles only fine, in that place is no compile fourth dimension error. You tin laissez passer on notice fifty-fifty access a nested static class from a non-static method, it absolutely fine.



But, only think, If the respond is that simple, so why this enquiry is ofttimes asked on Java interviews too Java certifications similar OCAJP or OCPJP? Well, the enquiry is a petty chip tricky too often asked confused candidates because the opposite is non true i.e. you lot tin laissez passer on notice access static members from non-static context but you cannot access a non-static variable or method from a static method inwards Java.

Why you lot cannot access a non-static variable or telephone weep upwardly a non-static method from a static method inwards Java? Well, this is because a static method forms a static context where alone static members tin laissez passer on notice survive accessed, but if you lot desire to a greater extent than explanation, I advise you lot become through ane of the to a greater extent than comprehensive resources similar non-static fellow member variables or methods cannot survive accessed from a static method inwards 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);   }  } 

$ javac Hello.java
Hello.java:11: non-static variable aNonStaticVariable cannot survive referenced from a static context
System.out.println(aNonStaticVariable);
^
Hello.java:12: non-static method aNonStaticMethod() cannot survive referenced from a static context
aNonStaticMethod();
^
2 errors


You tin laissez passer on notice run across that fifty-fifty though you lot tin laissez passer on notice access static members from a non-static method, the contrary is non true. If you lot endeavour to access a non-static variable or method or fifty-fifty a nested class, the compiler volition throw fault "non-static method XXXX cannot survive referenced from a static context".

So, similar a shot the big enquiry comes how tin laissez passer on notice you lot access a non-static variable or telephone weep upwardly a non-static method from a static method e.g. main() method inwards Java? let's discovery out.



How to access a non-static variable/method from a static method inwards Java

Well, in that place is a legitimate means to access whatsoever non-static fellow member from the static context inwards Java, past times creating instances. You demand to starting fourth dimension exercise an object of the shape whose non-static members or non-static method you lot desire to access. Once you lot exercise that, the compiler volition non bother you lot anymore every bit shown inwards 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 laissez passer on notice run across that all compile-time fault has gone later on access non-static variable too 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 shape inwards Java. See The Complete Java Masterclass for to a greater extent than details.

static method access a static variable or telephone weep upwardly a static method Can a Non Static Method Access a Static Variable/Method inwards Java?



That's all nearly whether a non-static method tin laissez passer on notice access static variable or method inwards Java or not. Of course, they tin laissez passer on notice but the contrary is non truthful i.e. you lot cannot access a non-static fellow member from a static context i.e. static method. The alone means to access a non-static variable from a static method is past times creating an object of the shape the variable belongs.

This confusion is the master copy argue why you lot run across this enquiry on substance Java interview every bit good every bit on substance Java certifications e.g. OCAJP too OCPJP exam. You volition discovery a lot of questions based on static concepts on OCAJP, peculiarly on Whizlabs Java 8 Exam Simulator hence it is rattling of import to cook this topic well.


Further Learning
answer)
  • Can you lot overload or override static method inwards Java? (answer)
  • Can nosotros declare a shape static inwards Java? (answer)
  • Can you lot brand an array volatile inwards Java? (answer)
  • Can you lot run a Java programme without main() method inwards Java? (answer)
  • Can you lot brand an abstract shape in conclusion inwards Java? (answer)
  • Can you lot override a mortal method inwards Java? (answer)
  • Top v Free Java 8 too Java ix courses for Programmers (courses)


  • Thanks for reading this article, if you lot similar this article so delight part amongst your friends too colleagues. If you lot convey whatsoever questions or feedback so delight driblet a comment.

    P. S. - If you lot are preparing for Java certification too desire to cook this topic good than reading a skillful substance Java majority like OCAJP Study Guide past times Mala Gupta can every bit good help a lot.  This is an fantabulous majority to acquire substance 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 Inward Java?"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel