1

I am being beginner trying to learn java basics and here in the this program I am confused why we can't reassign the value of class instance variable.enter image description here this is error in this program. Please guys help me out to figure it out. thanks

class AddInsideClassVar{
    int a = 3;
    int c;
    c = a + a;
    public static void main(String args[]){
        System.out.println();
    }
}
3
  • You can't execute statements directly inside the body of a class. Commented Feb 27, 2014 at 19:58
  • @SotiriosDelimanolis my question is that why we can't do that Commented Feb 27, 2014 at 19:59
  • 5
    Because the java language doesn't let you. Commented Feb 27, 2014 at 20:01

5 Answers 5

2

You may define fields within a class, but you are not allowed to put calculation statements outside of a method definition. A field declaration is of the form type; or type = value;

For example (from your code);

class AddInsideClassVar{
    static int a = 3;        // ok this is a declaration for a field (variable)
    static int c;            // ok, this is too
    //c = a + a;        // this is a statement and not a declaration. A field may be 
                      // declared only once
    static int d = a + a;    // this will work since it is part of a declaration.

    public static void main(String args[]){
        System.out.println("a=" + a + ", c=" + c + ", d=" + d);

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

Comments

1

You cannot execute c = a + a in that section. If anything you'd need to do

int a = 3;
int c = a + a;  

If you make these variables static then you could do

private static int a = 3;
private static int c;
static {
    c = a + a;
}

2 Comments

you can do something similar also if the variables are not static: private int c; { c = a + a; }
please dude try to explain it.
1

You can try this (just an example of workaround):

class AddInsideClassVar{
    static {
        int a = 3;
        int c;
        c = a + a;
        System.out.println(c);
    }

    public static void main(String args[]){

    }
}

1 Comment

hey I don't get it. You are using static block. I don't want that I need explanation why it actually not let us to use c = a+a;
0

You may be mixing your statics with instance variables. Here's how I would write this to not confuse myself:

public class AddInsideClassVar{
    int a;
    int c;

    public void doStuff() {
        a = 3;              
        c = a + a;
    }

    public static void main(String args[]){
        AddInsideClassVar instance = new AddInsideClassVar();
        instance.doStuff(); 
        System.out.println(c);
    }
}

a and c are instance variables. They are manipulated by a non-static method which requires an instance of the class to operate on. So, I create the instance in main(), then call the function to manipulate instance variables.

Comments

0

Explanation : int c = a + a is a declaration whereas " c = a + a ; " ( alone ) is a statement ; You have a point, it does not make much sense ;

class MyClass {
    int a = 3;
    int c = a + a; // Correct
}

or

class MyClass {
    int a = 3;
    int c;
    public void addition () {
        c = a + a; // Correct
    }
}

but not

class MyClass {
 int a = 3;
 int c;
 c = a + a; // Incorrect
}

NOTE : On the other, the Scala programming language ( compiles to JVM ) allows you to do the following :

scala> class MyClass { val a:Int = 3; 
var c:Int = _; 
c = a + a ;  // Correct
}
defined class MyClass

7 Comments

The second one is only correct if the variables are static.
It makes absolute sense. A class is a blurprint for an object. It just describes things. Methods actually contain the behavior, or execution.
this not working you are using c = a+a; that is not a valid statement in java.
Jarvish -- please redescribe what you are trying to accomplish (not what you have written). That will help us help you. Do you want the items a and c to be variables with an initial value, or constants? Or are you trying to change those variables and return them to a calling program with a method is called?
@Jarvish Because that's how Java works. Java only allows statements in specific places, so if you try to put statements in places where Java doesn't allow them then it doesn't work. Just like trying to write a Java program using plain English won't work.
|

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.