user6796700
user6796700

Reputation:

How do I create a proper Java instance?

I have created a public class helloworld and I am trying to create an object for class Abc. I have used "new" keyword to create an instance for Abc class.But still I am getting an error "non-static variable this cannot be referenced from a static context." in 4th line. How do I solve this problem?

public class helloworld {
    public static void main (String[] args)
    {
        Abc obj = new Abc();

    }
    class Abc
    {
        int i;

    }
}

Upvotes: 0

Views: 103

Answers (4)

Adriaan Koster
Adriaan Koster

Reputation: 16209

First of all: Heed Jon's advise. That said, you can quickly solve this particular problem by changing your inner class into a static class:

static class Abc {
    int i;
}

I know this might seem a bit weird, but you cannot refer to regular inner classes without first creating an instance of the outer class. Since the main method is static there is no instance of HelloWorld yet. This means it cannot refer to a regular (instance-bound) inner class only to a static inner class.

Arguably a better solution is to create an instance of your class first thing in your main method:

// class names should be camel case with an upper case first letter
public class HelloWorld { 

    public static void main (String[] args) {
        HelloWorld app = new HelloWorld();
        app.start();
    }

    // note that this is not a static method
    private void start() {
        Abc obj = new Abc();   
    }

    private class Abc {
        int i;    
    }

}

Upvotes: 1

Raúl Garcia
Raúl Garcia

Reputation: 302

The first awnser is correct but don't be easy. This is the easy way for a eclipse project o similar.

  1. Create a ABC class:

    public class Abc
    {
      int i;
    
    }
    
  2. Import in a Hello world class and declare static:

    import Abc;
    public class HelloWorld{
     private static Abc abc=new Abc();
     public static void main(String[] args){
      //do something 
     }
    }
    

All IDE that you use. Help you to import the class : )

Upvotes: 0

Stefan Warminski
Stefan Warminski

Reputation: 1835

Abc is an inner class of helloworld. To instantiate it you need an instance of helloworld

Abc abc = new helloworld().new Abc();

or make Abc a static inner class.

static class Abc {}

Upvotes: 0

anacron
anacron

Reputation: 6721

For simplicity, put class Abc in a different file and make it a public class.

helloworld.java

public class helloworld {
    public static void main (String[] args)
    {
        Abc obj = new Abc();

    }
}

Abc.java

public class Abc
{
    int i;

}

Or you can declare your class Abc as static:

public class helloworld {
    public static void main (String[] args)
    {
        Abc obj = new Abc();

    }
    static class Abc
    {
        int i;

    }
}

When you create an inner class, it follows the same rule as members of a class: Static members of a class cannot directly access Instance members. Thus, you will need to declare Abc as a static class.

PS: please use CamelCasing conventions to name your classes. Ex: Use HelloWorld instead of helloworld.


Hope this helps!

Upvotes: 2

Related Questions