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

Advertisement

Masukkan script iklan 970x90px

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

Kamis, 26 Maret 2020

"Can a non-static method access a static variable or telephone yell upwardly a static method" is 1 of the oftentimes asked questions on the static modifier inwards Java, the reply is, Yes, a non-static method tin access a static variable or telephone yell upwardly a static method inwards Java. There is no work alongside that because of static members i.e. both static variable in addition to static methods belongs to a degree in addition to tin endure called from anywhere, depending upon their access modifier. For example, if a static variable is private in addition to then it tin alone endure accessed from the degree itself, but you lot tin access a world static variable from anywhere. Similarly, a person static method tin endure called from a non-static method of the same degree but a world static method e.g. main() tin endure called from anywhere.

Here is a code instance to attempt out our indicate that a non-static method tin access both static variable in addition to 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 primary method");   }  }

You tin meet that this code compiles simply fine, at that topographic point is no compile-time error. You tin fifty-fifty access a nested static class from a non-static method, it is absolutely beautiful.




But, simply think, If the reply is that simple, in addition to then why this inquiry is oftentimes asked on Java interviews in addition to Java certifications similar OCAJP or OCPJP? Well, the work is a piffling flake tricky in addition to often asked confused candidates because the opposite is non exact, 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 inwards Java.

Why you lot cannot access a non-static variable or telephone yell 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 endure accessed, but if you lot desire to a greater extent than explanation, I advise you lot acquire through 1 of the to a greater extent than comprehensive resources similar non-static fellow member variables or methods cannot endure 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 endure referenced from a static context
System.out.println(aNonStaticVariable);
^
Hello.java:12: non-static method aNonStaticMethod() cannot endure referenced from a static context
aNonStaticMethod();
^
2 errors


You tin meet that fifty-fifty though you lot tin access static members from a non-static method, the reverse is non exact. If you lot elbow grease to access a non-static variable or method or fifty-fifty a nested class, the compiler volition throw fault "non-static method XXXX cannot endure referenced from a static context."

So, directly the big inquiry comes how you lot tin access a non-static variable or telephone yell upwardly a non-static method from a static method similar the main() method inwards Java? Let's uncovering out.




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

Well, at that topographic point is a legitimate agency to access whatsoever non-static fellow member from the static context inwards Java yesteryear creating instances. You demand to get-go create an object of the degree whose non-static members or non-static method you lot desire to access. Once you lot create 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 meet that all compile-time fault has gone afterward access non-static variable in addition to 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 degree inwards Java. See The Complete Java Masterclass for to a greater extent than details.

static method access a static variable or telephone yell 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 access a static variable or method inwards Java or not. Of course, they can, but the reverse is non true, i.e. you lot cannot obtain a non-static fellow member from a static context, i.e. static method. The alone agency to access a non-static variable from a static method is yesteryear creating an object of the degree the variable belongs.

This confusion is the primary argue why you lot meet this inquiry on substance Java interview every bit good every bit on substance Java certifications, e.g. OCAJP in addition to OCPJP exam. You volition uncovering a lot of questions based on static concepts on OCAJP, peculiarly on Whizlabs Java 8 Exam Simulator; hence, it is essential to create this topic well.


Further Learning
answer)
  • Can you lot overload or override static method inwards Java? (answer)
  • Can nosotros declare a degree static inwards Java? (answer)
  • Can you lot brand an array volatile inwards Java? (answer)
  • Can you lot run a Java computer programme without main() method inwards Java? (answer)
  • Can you lot brand an abstract degree concluding inwards Java? (answer)
  • Can you lot override a person method inwards Java? (answer)
  • Top five Free Java 8 in addition to Java ix courses for Programmers (courses)
  • 5 Free courses to larn object-oriented programming inwards Java (courses)
  • 10 Must Read books to larn Java in-depth (books)
  • 50+ Java Interview Questions for beginners (interview questions)

  • Thanks for reading this article, if you lot similar this article, in addition to then delight portion alongside your friends in addition to colleagues. If you lot convey whatsoever questions or feedback, in addition to then delight drib a comment.

    P. S. - If you lot are preparing for Java certification in addition to desire to create this topic good than reading a skillful substance Java majority like OCAJP Study Guide yesteryear Mala Gupta can besides assistance a lot.  This is an fantabulous majority to larn substance Java fundamentals fifty-fifty if you lot are non preparing for exams.