in java any abstract variables there? i worked this abstract variables in constructor i am nort sure but i think the constructor support static variables.pls clarify my doubt
+6
A:
In java only classes and methods can be abstract. Variable declarations cannot. However you can have variable declarations whose types are abstract. See example:
public abstract class MyClass { // allowed
public abstract myMethod(); // allowed
public MyClass instance; // allowed
public abstract MyClass instance; // NOT ALLOWED!!
}
naikus
2010-08-25 04:57:47
+1
A:
The language specifacation lists 7 types of variables:
- class variables - declared as static within a class declaration
- instance variables - declared within a class declaration without using the static keyword
- array components - like
i[2]
when we create an array likeint[] i= new int[5]
- method parameters - name argument values passed to a method
- constructor parameters - name argument values passed to a constructor
- exception-handler parameter - created each time an exception is caught
- local variables - declared in a block (
{ }
) or for statement
You can use all variable types (except #4) in a constructor:
class Demo {
static int demo1 = 0; // class variable
int[] demo2 = new int[5]; // instance variable
Demo(int demo3) { // constructor parameter
try {
int demo4 = // local variable
demo2[2]; // array component
} catch(RuntimeException demo5) { // exception-handler parameter
}
demo2 = new int[]{Demo.demo1}; // using class and instance variable
// in a constructor
}
int method(int demo6) { // method parameter
}
}
The abstract
keyword is not allowed for variable declaration.
Andreas_D
2010-08-25 07:52:32