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");
}
}
}
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.
Comments