views:

82

answers:

2

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
+1  A: 

The language specifacation lists 7 types of variables:

  1. class variables - declared as static within a class declaration
  2. instance variables - declared within a class declaration without using the static keyword
  3. array components - like i[2] when we create an array like int[] i= new int[5]
  4. method parameters - name argument values passed to a method
  5. constructor parameters - name argument values passed to a constructor
  6. exception-handler parameter - created each time an exception is caught
  7. 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