• 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 blank variable and Inner class

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all.
I came across some strange thing I couldn't figure out why.
read the following code. it compiles fine with the inner class declaration and creation commented out. but once you take the comments out. it gave you a compiler error that says "final blank variable aaa may not have been initialized" but clearly it's initialized in the User() constructor. can anyone tell me Why?
thanks a lot!!!
------------------------------
class User {
final int aaa;
User() {
aaa = 109;
}
/* take the comments off - won't compile
class User_I { int vv = 0;}
static class User_SI {
static int v = 0;
}
*/
public static void main(String[] args) {
User u = new User();
/* take the comments off - won't compile
User_I ui = new User().new User_I();
User_SI usi = new User().new User_SI();
User_SI usi2 = new User.User_SI();
*/
}
}
-----------------------
chun
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is compiling fine.
[This message has been edited by Pragya Prakash (edited February 06, 2001).]
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which compiler are you using..?
It compiles for me. I'm using JDK 1.3.0

------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using an OLD compiler jdk1.1. so maybe that's problem?
I appreciate your trying.
chun
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pragya Prakash:
it is compiling fine.
[This message has been edited by Pragya Prakash (edited February 06, 2001).]


Hi, I think it is wrong in your code to create an object of a non-static inner class in main. The reason is that main is a static method, has no access to the non-static variables, methods as well as inner classes in the enclosing class.
 
Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jenny.
I assure you there is nothing wrong with creating non-static inner class objects in main(). you can try by getting rid of all the lines that have the variable aaa .then it will compile fine.
chun
 
reply
    Bookmark Topic Watch Topic
  • New Topic