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!!!

11
  • 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. Commented May 20, 2014 at 9:46
  • Please follow this tutorial. Commented May 20, 2014 at 9:47
  • 1
    You (accidentally?) declared two classes in this file. For absolute beginners, try to stay with one class per file. Commented May 20, 2014 at 9:49
  • 2
    You should have MyClass as static. Commented May 20, 2014 at 9:49
  • 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. Commented May 20, 2014 at 9:53

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You could make MyClass public:

public static class MyClass{
    ...
}

3 Comments

You should do it, because you use it within static method, so it should not be inner of some concrete instance of java_main, I think, that should be mentioned in answer.
Fixes the problem, but not at all a good answer to help an absolute beginner understand what is going on.
You should make static or public?
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;
    }
}

Comments

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;
    }
}

3 Comments

You should not just write code, please explain OP, why is it impossible to create instance of non-static inner class without instance of outer class.
@DmitryGinzburg: +1. But in this case, there is no need to even go to inner classes at all. OP likely just stumbled there by accident.
there are atleast 5 answers here explaining that ;)
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;
    }
}

}

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.