• Post Reply Bookmark Topic Watch Topic
  • New Topic

why variables from super(....); must be declared static?

 
Andrei Mura
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this:

The compiler says that rows and columnNames must be declared static. Why this?
Thank advance.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please supply the full details of the error message.
 
Andrei Mura
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use Eclipse IDE. This is a compiler error, so the error is:

Multiple markers at this line
- Cannot refer to an instance field columnNames while explicitly invoking a
constructor
- Cannot refer to an instance field rows while explicitly invoking a constructor

But i think that i've understand. Probably i can't use instance variables because the first line executed is super(....). So is needed that the fields to be static.
what is your opinion?
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are introducing a circular dependency. That is why there is a problem.

You are trying to set the value of those two fields from the value of those fields.
 
sandeep kumar singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because when call to super is encountered inside constructor yet the instance is not created for that class....so thats is the reason you cant use any instance variable iinside super but since static variable does not exist with respect any instance so they can be used.
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep is right. When a constructor is created, the following occurs (in order):
1) the call to super()
2) initialization of fields and execution of initializer blocks
3) the actual body of the constructor

Since columnNames and rows don't get initialized until step 2 they will have no value at step 1.

But why do you need to store the column names and data in the JTable? It's already stored in the model, and the JTable can simply retrieve it from it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!