how to reffer non static variable from static method?

borra

Q)i got one compile error 10::non-static variable this can not be refferenced from the static content

what to do now??

class Computer
{    
  void method()    
  {    
     System.out.println("this i objects");
  }


   public static void main(String[] args)    
    {    
      Laptop mtd = new Laptop();
      Computer mtd1 = new Computer();
      mtd.method();
      mtd1.method();
     }  


class Laptop
  {    
    void method()    
     {    
      System.out.println("using laptop method");         
     }    
  }    
}
Stephan Henningsen

Laptop is an inner class of Computer, thus you have to instantiate Laptop from a Computer instance. Or you can mark your inner Laptop class as static, then you can instantiate it directly. My example demonstrates both approaches:

class Computer
{

  public static void main(String[] args)
  {
      Computer computer = new Computer();
      computer.method();

      // Instantiate normal inner class from instance object.
      Laptop laptop = computer.new Laptop(); // Or: new Computer().new Laptop();
      laptop.method();

      // Instantiate static inner class directly.
      StaticLaptop staticLaptop = new StaticLaptop();
      staticLaptop.method();
  }

  void method()    
  {
      System.out.println("I'm Computer!");
  }


  class Laptop
  {
      void method()
      {
        System.out.println("I'm Laptop!");  
      }
  }

  static class StaticLaptop
  {
      void method()
      {
        System.out.println("I'm StaticLaptop!");  
      }
  }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to access global non static variable from static method

From Dev

Access static variable from non static method in Swift

From Java

How to achieve static method from non-static interface method?

From Java

How to print a non-static method from a static method?

From Dev

How to return non static variable from static method ES6 Class

From Dev

How to enter a non static method from a static one

From Dev

How to resolve non-static method errors from static context?

From Dev

how to change the value of a static variable from a static method of another class

From Dev

How to reference static method from class variable

From Dev

Calling a non static method from a in a static context

From Dev

Access static method from static variable

From Java

Java: how to call non static method from main method?

From Dev

Call non static method from different non static class

From

How to call non-static method from static method of same class?

From Dev

How can I call a trait's non-static method from a static method with PHP7?

From Java

Why is accessing a static method from a non-static method bad?

From Dev

non-static method cannot be referenced from a static method

From Dev

Why is accessing a non static method from static method is Bad design

From Dev

call non static method from static method in android

From Dev

Calling non-static method in static method in Java (Non-Static Variable Error)

From Dev

Store non-static method in variable Java

From Java

Using a non static variable on a static method through an object? Java

From Java

Can static method access non-static instance variable?

From Dev

How to call non static method from main class

From Dev

How to display the return value from a non static method in C#

From Dev

NUnit: How to pass TestCaseData from a non-static method?

From Dev

How to call non-static java method from c++?

From Dev

How to pass a non static method from a specific class as parameter to Function

From Dev

How to call a non-static method from the main activity to a fragment?

Related Related

  1. 1

    How to access global non static variable from static method

  2. 2

    Access static variable from non static method in Swift

  3. 3

    How to achieve static method from non-static interface method?

  4. 4

    How to print a non-static method from a static method?

  5. 5

    How to return non static variable from static method ES6 Class

  6. 6

    How to enter a non static method from a static one

  7. 7

    How to resolve non-static method errors from static context?

  8. 8

    how to change the value of a static variable from a static method of another class

  9. 9

    How to reference static method from class variable

  10. 10

    Calling a non static method from a in a static context

  11. 11

    Access static method from static variable

  12. 12

    Java: how to call non static method from main method?

  13. 13

    Call non static method from different non static class

  14. 14

    How to call non-static method from static method of same class?

  15. 15

    How can I call a trait's non-static method from a static method with PHP7?

  16. 16

    Why is accessing a static method from a non-static method bad?

  17. 17

    non-static method cannot be referenced from a static method

  18. 18

    Why is accessing a non static method from static method is Bad design

  19. 19

    call non static method from static method in android

  20. 20

    Calling non-static method in static method in Java (Non-Static Variable Error)

  21. 21

    Store non-static method in variable Java

  22. 22

    Using a non static variable on a static method through an object? Java

  23. 23

    Can static method access non-static instance variable?

  24. 24

    How to call non static method from main class

  25. 25

    How to display the return value from a non static method in C#

  26. 26

    NUnit: How to pass TestCaseData from a non-static method?

  27. 27

    How to call non-static java method from c++?

  28. 28

    How to pass a non static method from a specific class as parameter to Function

  29. 29

    How to call a non-static method from the main activity to a fragment?

HotTag

Archive