• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem on invoking a method inside a class  RSS feed

 
Greenhorn
Posts: 16
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Please read the following code to get to know my question:

 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is already syntax defined to be able to support that and the syntax distinguishes between whether you want the code to be called when the class is loaded in memory or when an instance of the class is created.
So for the first part you want

static {
A.show(); // class initializer, when class is loaded
}

and the for the second it's just

{
ia.showInnerClass(); //instance initializer, when object is created
}
 
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to go through the grammar; in the case where the () are omitted, that is a call to a field, which does not exist. Even if that field did exist, that would not be a complete statement, and it would not be permissible.
In the other case, A.show() is a statement and statements must be inside methods (or initialisers).
 
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a field showInnerClass
Put the statements: A.show(); and ia.showInnerClass; in some method(s)
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sveto Koychev wrote:Create a field showInnerClass
Put the statements: A.show(); and ia.showInnerClass; in some method(s)
I do not think that will help with the current problem.
 
Xiaolong Bao
Greenhorn
Posts: 16
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You would have to go through the grammar; in the case where the () are omitted, that is a call to a field, which does not exist. Even if that field did exist, that would not be a complete statement, and it would not be permissible.
In the other case, A.show() is a statement and statements must be inside methods (or initialisers).


Thanks a lot for your point!
Sometimes, I was really flurried while trying to combine all the learned things together...
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Xiaolong Bao wrote: . . . trying to combine all the learned things together...
Don't worry; there are many many things to learn and everybody has such problems

…and You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!