0

Is it possible to create variable for variable Names ? like this

class example {   
  String VAR1 = "id";
  String VAR2 = "mark";

   public foo()
    {
     int VAR1;
     int VAR2;
    }
}

Or Say I'm having nested class Parent and Child.

class Parent { 
   char a;
   //some variables

   class child {
     //char b = Parent.a;
   }
}

I'm having a lot of class like this.So I need to replace that 'Parent' in 'Parent.a' with something like a MACRO so that I don't want to change every 'Parent' in its child classes individually.

Is there any possible solution for this ?

shiva
  • 463
  • 2
  • 7
  • 9
  • 2
    There isn't in Java, but there is usually no need - you're usually better off with rethinking your design. What exactly are you trying to _do_? – Mat Apr 09 '12 at 11:16
  • Java reflection api might serve your purpose. –  Apr 09 '12 at 11:18
  • not very clear with your problem. But you can not declare variables with same name in the same scope or overlapping scope. So, your first part will not work. – vpv Apr 09 '12 at 11:18
  • @Shiva, if **public foo()** is the constructor of the class, then its name should match with class name. – vpv Apr 09 '12 at 11:22
  • @Mat I have to copy paste lots of same codes in my program.So i dont want to rename variables individually. – shiva Apr 09 '12 at 11:33
  • 1
    @shiva: if you need to copy/paste lots of code, you really need to rethink your design. Please, explain exactly what you are trying to _do_ (and edit that into your question); it is not clear at all at this point. – Mat Apr 09 '12 at 11:35
  • @V.P.Verma its not a constructor just a method. – shiva Apr 09 '12 at 11:37
  • @Shiva, then the function must have a return type, at least void. – vpv Apr 09 '12 at 11:44
  • this question is incomprehensible. – akappa Apr 09 '12 at 12:12
  • You need to take a step back from your question about "how" to accomplish something and give us the "what" and the "why". What are you trying to accomplish? Why would you want this virtual variable name? This is just not a proper Java pattern. – Gray Apr 10 '12 at 13:46

3 Answers3

0

Your first example is not possible in Java. The second example is valid if you change your code:

class Parent {
    char a;

    class child {
        char b = a;
    }
}
Sandro
  • 1,266
  • 2
  • 13
  • 25
  • Thanks.I know that and I'm asking is there anything to do something like that. – shiva Apr 09 '12 at 11:36
  • Can you describe the problem you are trying to solve by using a variable for variable names? For me this sounds like a job for a HashMap. – Sandro Apr 09 '12 at 13:03
0
class Parent { 

       // Main parent
       char a;
       //some other fields

       // The child class
       class Child {

         // The main child
          char a = Parent.a;
         //some other fields

       }

       // this is what you need
       public char getChild() {
           Child child = new Child();
           return child.a;
       }
    }
GingerHead
  • 8,130
  • 15
  • 59
  • 93
0

If you'd like a convenient way to rename variables, then use an IDE that lets you do just that.

This kind of things are (or, at least, should be) out of the realm of programming language design.

akappa
  • 10,220
  • 3
  • 39
  • 56