1
class A {
    public int someVar;
    someVar = 123;      /* cannot find symbol */
}

Why cant the language see the variable I just declared? And is this unique to Java or this is true in all classful OOP languages?

djechlin
  • 59,258
  • 35
  • 162
  • 290
1der
  • 185
  • 1
  • 3
  • 12
  • 12
    Class bodies cannot contain executable code. – Sotirios Delimanolis Sep 03 '13 at 20:06
  • @SotiriosDelimanolis no, false; you could write `public Foo foo = new Foo()`, and the constructor contains arbitrary code. – djechlin Sep 03 '13 at 20:08
  • 1
    @djechlin That's an instance initialization statement. Have a look [here](http://stackoverflow.com/questions/1994218/instance-variable-initialization-in-java). It's not the same as executable code. – Sotirios Delimanolis Sep 03 '13 at 20:10
  • @djechlin That is object initialization – Trikaldarshiii Sep 03 '13 at 20:11
  • so is this unique to Java or this applies to all OOP languages? – 1der Sep 03 '13 at 20:20
  • @Photon object initialization is executable code. – djechlin Sep 03 '13 at 20:23
  • @1der It's likely on a per-language basis, but note that python doesn't allow you to put code outside the methods at all. You must define them all with self.variable. They may also be defined outside of the constructor for the first time for a class. Every language does OO differently – Cruncher Sep 03 '13 at 20:31

5 Answers5

9

In Java, you can declare instance variables inside a class but outside a method, but statements such as someVar = 123; must be within a method (or a constructor, a static initializer, or an instance initializer).

rgettman
  • 176,041
  • 30
  • 275
  • 357
6

You can't have arbitrary statements in a class definition. You can either immediately assign the variable in the declaration:

public int someVar = 123;

Or, assign it in a constructor or other instance scope:

public class A {
  public int someVar;

  public A() {
    someVar = 123;
  }
}
//Or...
public class B {
  public int someVar;
  { someVar = 123; }
}

Note that the second technique uses an instance initializer, which is not always the most immediately clear piece of code.

Community
  • 1
  • 1
Thorn G
  • 12,620
  • 2
  • 44
  • 56
2

You can not declare someVar = 123; statement directly in class.
It should be instance block or in method or in constructor

 class A {
    public int someVar = 123;
}

or

class A {

    public int someVar ;
    {
          somevar = 123;
    }
}

or

class A {

        public int someVar ;
        A(){
              somevar = 123;
        }
    }

or

class A {

        public int someVar ;
        public void method(){
              somevar = 123;
        }
    }
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
1

But, oddly, you can code

public class AFunnyClass {
    public int someVar;
    {
        someVar = 123;
    }
    public static int anotherVar;
    static {
        anotherVar = 456;
    }
    public int yetAnotherVar = 789;
    public static int adNauseum = 101112;
}
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

Maybe you want to use someVar as a static variable. Then you should declare it also static. Using it like:

class A {
    static public int someVar = 123; 
}

That means someVar isn't just a member of an instance of this Class, it becomes a member of the class it self. That also means someVar will be instantiated just once and can be used by all instances of this Class

Tschwen
  • 288
  • 3
  • 7