1

My question is regarding the declaration and value assignment rules in Java. When writing the fields we can declare and assign values together but we cannot do the same separately.

E.G.:

class TestClass1 {
    private int a = 1;
    private int b ;
     b= 1;
    private int sum;

    public int getA() {
        return a;
    }

    public int getB() {
        return b;
    }

    public int getSum() {
        sum = a + b;
        return sum;
    }

}

public class TestClass {
    public static void main(String[] args) {
        TestClass1 testClass1 = new TestClass1();

        System.out.println("total =" + testClass1.getSum());
    }    

}    

Here in line:

private int a = 1; 

We are able to declare a as a private int and assign a value 1 to it. But in case of:

private int b ;
b= 1; 

Eclipse does not allow this to happen and throws an error. Kindly explain the logic behind this.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jagvirsingh5
  • 59
  • 1
  • 9
  • 1
    yes you can not, this is a basic syntax of Java, a member field can be `initialized` when declare, and any statement should be in method or static block! – Rugal Jan 08 '14 at 08:37
  • Other than it's pointless (in the case of primitives)? Because that's how the language was designed. See: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html – Brian Roach Jan 08 '14 at 08:43

5 Answers5

5

Code inside a class, but outside a function, is purely declarations. It does not get "executed". It simply declares what fields a class contains.

The reason you can do the shorthand private int a = 1; is just syntactic sugar that the Java language allows. In reality, what happens is that the a = 1 part is executed as part of the constructor. It's just easier to read and write when it is next to the variable declaration.

It's something nice that the Java langage creators allowed. Not every language allows that, look at C++ as an example that does not always allow it.

Cubicle Dragon
  • 305
  • 1
  • 9
  • Cubicle Dragon :- So should I embrace it as a practice never to declare a value when inside a class, but outside a function? Always see to it that I should have a constructor which will have all the declarations housed in it? Tx – jagvirsingh5 Jan 08 '14 at 09:03
2

you have to put b=1; inside a method or put this inside a constructor.

you are getting this error since you can't do any thing other than declaration(private int a= 1; ) in class level.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • That is a comment , he is asking the reason for that syntax error . – AllTooSir Jan 08 '14 at 08:37
  • Hi all,as I mentioned I know the issue. I know b=1 should either be assigned at the time of declaration or inside a method/constructor. I am asking the explanation ? Coz if declaration is prob outside method then even private int a = 1 ; should have been an issue. – jagvirsingh5 Jan 08 '14 at 08:50
1

It's just a question of syntax in Java. In the example you show, you try to affect a value to b in the member declaration part of your class. This is not allowed by the syntax of Java. You can only do it when you declare your attribute, in the body of a method or in a static block if your attribute is static e.g. :

private static int b;
static {
    b = 1;
}
Julien
  • 2,544
  • 1
  • 20
  • 25
1

It is only a syntax problem. You can do it like this :

private int b ;

{  // <- Initialization block
  b= 1;
}

See What is an initialization block?

Community
  • 1
  • 1
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

You are unable to write logic directly in the class. You should move it to the constructor.

Java only allow declaration within the class and outside any method. Declarations like int b = 1; will be executed when you initialize a new Object.

AppX
  • 528
  • 2
  • 12