• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Final once again

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
There are 2 classes � class x1 AND class checkfinal � Please notice the differences.
And help me out.
class A
{
public static void main(String[] args)
{ final int a = 20;
System.out.println("Hello World!");
}
}
class B
{
};
class X1 extends A
{
public static void main(String[] args)
{
//A a = new A();//line 1
int a = 15;
System.out.println("Hello !");
}
};
DOUBT: This compiles. But if you put A a = new A()at line 1 , ie , instantiate A inside X1 then threre is a compile error �a is already defined in this method.� -- why
-------
class checkfinal
{
public static void main(String[] args)
{
B b = new B();
b.call();

System.out.println("Hello World!");
}
}
class A
{
final int i = 10;
};
class B extends A // {
A a = new A();// LINE --- 1
int i = 20;
void call()
{
for (;i==20 ; )
{
//
}
}


};
DOUBT: HERE IT IS THE SAME THING BUT IT COMPILES . EVEN IF I PUT A a = new A() , the code compiles. why?

Thanks in advance
Padmini
[This message has been edited by padmini Babu (edited June 24, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has nothing to do with final. It only has to do with your choice of variable names.
In the first case you have two variables both named "a". This causes a problem for the compiler - so it complains. In the second case you have politely converted the variable name to "i" and eliminated the problem.
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
This has nothing to do with final. It only has to do with your choice of variable names.
In the first case you have two variables both named "a". This causes a problem for the compiler - so it complains. In the second case you have politely converted the variable name to "i" and eliminated the problem.


********************
Thnaks cindy,
Well that was a very silly mistake of mine. Hi ranchers, pl ignore this question.
Padmini
 
reply
    Bookmark Topic Watch Topic
  • New Topic