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

"Can a non-static method access a static variable or telephone band a static method" is ane of the oft asked questions on the static modifier inwards Java, the reply is, Yes, a non-static method tin plow over notice access a static variable or telephone band a static method inwards Java. There is no work alongside that because of static members i.e. both static variable as well as static methods belongs to a bird as well as tin plow over notice live on called from anywhere, depending upon their access modifier. For example, if a static variable is private as well as so it tin plow over notice exclusively live on accessed from the bird itself, but you lot tin plow over notice access a world static variable from anywhere. Similarly, a somebody static method tin plow over notice live on called from a non-static method of the same bird but a world static method e.g. main() tin plow over notice live on called from anywhere.

Here is a code representative to seek our hollo for that a non-static method tin plow over notice access both static variable as well as 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 plow over notice run into that this code compiles simply fine, at that spot is no compile-time error. You tin plow over notice 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, as well as so why this query is oft asked on Java interviews as well as Java certifications similar OCAJP or OCPJP? Well, the work is a petty fleck tricky as well as often asked confused candidates because the opposite is non exact, i.e. you lot tin plow over 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 band a non-static method from a static method inwards Java? Well, this is because a static method forms a static context where exclusively static members tin plow over notice live on accessed, but if you lot desire to a greater extent than explanation, I propose you lot become through ane of the to a greater extent than comprehensive resources similar non-static fellow member variables or methods cannot live on 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 live on referenced from a static context
System.out.println(aNonStaticVariable);
^
Hello.java:12: non-static method aNonStaticMethod() cannot live on referenced from a static context
aNonStaticMethod();
^
2 errors


You tin plow over notice run into that fifty-fifty though you lot tin plow over notice access static members from a non-static method, the reverse is non exact. If you lot travail 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 on referenced from a static context."

So, forthwith the big query comes how you lot tin plow over notice access a non-static variable or telephone band a non-static method from a static method similar the main() method inwards Java? Let's discovery out.




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

Well, at that spot is a legitimate agency to access whatsoever non-static fellow member from the static context inwards Java yesteryear creating instances. You demand to showtime create an object of the bird whose non-static members or non-static method you lot desire to access. Once you lot produce that, the compiler volition non bother you lot anymore, equally 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 plow over notice run into that all compile-time mistake has gone later access non-static variable as well as 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 bird inwards Java. See The Complete Java Masterclass for to a greater extent than details.

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



That's all almost whether a non-static method tin plow over notice 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 exclusively agency to access a non-static variable from a static method is yesteryear creating an object of the bird the variable belongs.

This confusion is the primary argue why you lot run into this query on heart as well as soul Java interview equally good equally on heart as well as soul Java certifications, e.g. OCAJP as well as OCPJP exam. You volition discovery a lot of questions based on static concepts on OCAJP, especially on Whizlabs Java 8 Exam Simulator; hence, it is essential to create this theme well.


Further Learning
answer)
  • Can you lot overload or override static method inwards Java? (answer)
  • Can nosotros declare a bird 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 bird terminal inwards Java? (answer)
  • Can you lot override a somebody method inwards Java? (answer)
  • Top five Free Java 8 as well as Java ix courses for Programmers (courses)
  • 5 Free courses to acquire object-oriented programming inwards Java (courses)
  • 10 Must Read books to acquire Java in-depth (books)
  • 50+ Java Interview Questions for beginners (interview questions)

  • Thanks for reading this article, if you lot similar this article, as well as so delight portion alongside your friends as well as colleagues. If you lot accept whatsoever questions or feedback, as well as so delight drib a comment.

    P. S. - If you lot are preparing for Java certification as well as desire to create this theme good than reading a practiced heart as well as soul Java majority like OCAJP Study Guide yesteryear Mala Gupta can equally good aid a lot.  This is an fantabulous majority to acquire heart as well as 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

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel