1

This is my first day learning java (on my own), so I need some help. This is my code:

public class java_main {

    public static void main(String[] args) {
        MyClass my = new MyClass(3,4);
        MyClass your = new MyClass();   
    }

    public class MyClass {
        public int a,b; 

        public Myclass(int i, int j) {
            a = i;
            b = j;
        }

        public Myclass() {
            a = 1;
            b = 2;
        }
    }
}

I'm getting this error:

No enclosing instance of type java_main is accessible. Must qualify the allocation with an enclosing instance of type java_main (e.g. x.new A() where x is an instance of java_main).

Any suggestions? Thanks in advance!!!

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
a_user
  • 207
  • 3
  • 13
  • 2
    your main method must be within the class. EDIT: missed a part of your code, I must 've scrolled down too far you can only have 1 public class in a Java file (which must have the same name as the file itself) even if you put two classes in one file, they shouldn't be nested. yes, you can have an anonymous class within another class, but that's not what you're doing here. – Stultuske May 20 '14 at 09:46
  • Please follow [this tutorial](http://docs.oracle.com/javase/tutorial/). – Maroun May 20 '14 at 09:47
  • I think there is no error. when i run your code in my system it's work fine – Keval Trivedi May 20 '14 at 09:47
  • 1
    You (accidentally?) declared two classes in this file. For absolute beginners, try to stay with one class per file. – Thilo May 20 '14 at 09:49
  • what you did is declare an inner class and instantiate in outer class,,,,,, – Florescent Ticker May 20 '14 at 09:49
  • 2
    You should have `MyClass` as `static`. – Maroun May 20 '14 at 09:49
  • @Stultuske What do you mean? – a_user May 20 '14 at 09:50
  • @MironBalcerzak yes there is a error. my mistake. – Keval Trivedi May 20 '14 at 09:51
  • Maroun Maroun is right - Problem Solved! Thanks a lot! – a_user May 20 '14 at 09:52
  • 1
    @a_user I missed part of your code, scrolled down too far, I updated my response. but, as Thilo suggested: start at the beginning. – Stultuske May 20 '14 at 09:53
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:38

6 Answers6

3

The problem you have is that you have enclosed in java_main class MyClass

public class java_main {

  public class MyClass {

  }

}

Remove the java_main, to get valid result.

public class MyClass {

   public static void main(String[] args) {
    MyClass my = new MyClass(3,4);
    MyClass your = new MyClass();   
   }


    private final int a,b; 

     public Myclass() {
        this(1,2);
    }

    public Myclass(int a, int b) {
        this.a = a;
        this.b = b;
    }

}

The ussue you have is casued that you have to create first instance of outer class in way to be create instance of inner.

public static void main(String[] args)
   {
       java_main outer = new java_main();
       Myclass my   = outer.new Myclass(3,4);
       Myclass your = outer.new Myclass();   
   }

The key word static apply to parts of code that is not part of object it is only enclosed by its path (a method must be in class).

Try to find a tutorial that will guide you.

2

You could make MyClass public:

public static class MyClass{
    ...
}
tdeo
  • 119
  • 4
0

It works ...

public class java_main{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Myclass my = new Myclass(3,4);
        Myclass your = new Myclass();  
        System.out.println("keval");
    }

}
 class Myclass
{
    public int a,b; 

    public Myclass(int i, int j)
    {
        a = i;
        b = j;
    }

    public Myclass()
    {
        a = 1;
        b = 2;
    }
}
Keval Trivedi
  • 1,290
  • 2
  • 13
  • 29
0

change your code to this:

public class java_main {

   public static void main(String[] args)
   {
       Myclass my = new Myclass(3,4);
       Myclass your = new Myclass();   
   }
}

class Myclass
{
    public int a,b; 

    public Myclass(int i, int j)
    {
        a = i;
        b = j;
    }

    public Myclass()
    {
        a = 1;
        b = 2;
    }
}
0

Just try this,

public class java_main {

public static void main(String[] args) {
    MyClass my = new java_main().new MyClass(3, 4);
    MyClass your = new java_main().new MyClass();
}

public class MyClass {
    public int a, b;

    public MyClass(int i, int j) {
        a = i;
        b = j;
    }

    public MyClass() {
        a = 1;
        b = 2;
    }
}

}

Better approach is move the public class into separate file Class name should start with Capital letter as per Java naming standard.

Here, I just created an instance of java_main and by using that instance created an instance for MyClass. This is an approach if you don't want to make MyClass as static inner class.

else make MyClass as static inner class it will work but it depends on use case,

public class java_main {

public static void main(String[] args) {
    MyClass my = new java_main().new MyClass(3, 4);
    MyClass your = new java_main().new MyClass();
}

public class MyClass {
    public int a, b;

    public MyClass(int i, int j) {
        a = i;
        b = j;
    }

    public MyClass() {
        a = 1;
        b = 2;
    }
}

}

yuvi
  • 37
  • 5
0

The problem you are having is you cannot reference non-static variables (instances of MyClass) from the static context (main menu in java_main class).

Either you change your MyClass like this,

public static class Myclass

Or take out MyClass out of the java_main class. And remove the public access modifier from the MyClass. Because you cannot have two public classes in the same file.

public class java_main {

    public static void main(String[] args)
    {
        Myclass my = new Myclass(3,4);
        Myclass your = new Myclass();   
    }
}
class Myclass
{
    public int a,b; 

    public Myclass(int i, int j)
    {
        a = i;
        b = j;
    }

    public Myclass()
    {
        a = 1;
        b = 2;
    }
}

Hope this helps for you or someone else.

Cheers!

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54