0

This is my code:

public class MyClass {
    int x;
    MyClass m1 = new MyClass();
    m1.x=10;
}

Why does line m1.x=10; result in error?

4
  • 4
    Please share the full and exact error message - to me, this looks like completely invalid code Commented Feb 7, 2019 at 9:18
  • 1
    your code makes no sense and would quite fast lead to an infinite loop of initializations of MyClass. an instance variable can be initialised in class, if you do it correctly. Commented Feb 7, 2019 at 9:19
  • public class MyClass { int x = 10; } Why does this code work fine and the above code dosen't? That's my confusion Commented Feb 7, 2019 at 9:32
  • You need a tutorial in classes, objects , variables (including local variables). Commented Feb 7, 2019 at 12:39

4 Answers 4

2

if you want to assign value to the variable x, the line initializes it should be placed in specific method like below. did you intend to do this?

public class MyClass
{
    int x;  

    public static void main(String[] args)
    {
        MyClass m1 = new MyClass();
        m1.x = 10;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use instance initialization block:

public class MyClass {
    int x;    // define x variable
    MyClass m1 = new MyClass();    // initialize m1 variable
    {
        m1.x=10;    // assign 10 to m1.x (this is assignment statement)
    }
}

Out of block you can do only defining and initializing variables, not assignment statement.

2 Comments

Isn't initializing variables same as assignment statement?
@sahanir No. Initializing is special type of assignment that can be done only at the time of declaration
0

There are two errors in your code:

MyClass m1 = new MyClass();

This is an infinite recursion.

m1.x=10;

This is a statement, and as such should be within a method or constructor, not the class body.

5 Comments

MyClass m1 = new MyClass();. This line is fine. You can compile and check it.
We might have different definitions of fine then, because I don't consider Exception in thread "main" java.lang.StackOverflowError to be desirable behavior
That only means that main function is missing. Add a main function and MyClass m1=new MyClass() will not give any error.
Wrong: public class Test { Test test = new Test(); public static void main(String[] args) { new Test(); } }
Or did you mean to say "when you move the class instation to the main method" it won't given an error? Regardless, my point is still valid: your original code sample has an infinite recursion problem.
0

The problem here is the code m1.x=10; This line shows an operation or behavior which is only permissible within a block of code.

Valid Code for this operation.

public class MyClass {
    int x;

    public void assignOperation() {
        this.x = 10;
    }

    public static void main( String[] args ) {
        MyClass myClass = new MyClass();
        myClass.assignOperation();

        System.out.println( "Assigned value is " + this.x )
    }
}

Another valid example outside of a method but within the class body will be :

public class MyClass {
    static int x;

    static {
        x = 10;
    }

    public static void main( String[] args ) {
        System.out.println( "Assigned value is " + x )
    }
}

making variable x static doesn't need us initialize an object of the class MyClass.

2 Comments

class MyClass { int x=10; public static void main(String args[]) { } } Then why does this work ?
This works because now x=10; is at class level and main method isn't accessing it. No compiler errors will be issued. If static method like main tries to access a non-static variable without using an object of the class, then compiler will complain.

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.