• Post Reply Bookmark Topic Watch Topic
  • New Topic

Very basic but tough question  RSS feed

 
Abhinaba Basu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know we can declare a variable in a class scope. Like below:





But we can't call any method in a class scope. Like below:



It will give some error. Upto that it is OK. But my question is why java is not allowing us call a method in class scope? Very basic question but I am confused about that. Can anyone please help me to answer this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General (beginner).
 
Abhinaba Basu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its ok. but can u answer this? It is basic question, but anyone has any idea about that? Why java is not allowing. Dont answer like, main only can call a method, so a method call should be inside main, or inside another method like this. I need the proper answer why java is not allowing us? We can call constructor also. Is not it? So what is java's problem on that(method calling)?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So suppose it was possible to have method calls directly inside a class body - could you tell us what you think that should mean? When should that code be executed?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you can do that, just get the syntax right:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint that explains why Jeff's code works: instance initializer
 
Abhinaba Basu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is static initializer. But dr we can call methods like,



but the return type of println is void. So we can't initialize anything using this.

But method call is possible, which returns a vlue.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhinaba Basu:

but the return type of println is void. So we can't initialize anything using this.


Yo and behold, you can:
 
Vinay Thippeswamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cant because main is where your code starts excuting. if u put println directly inside a class without a main there is no execution taking place. call a function called go(); inside main and then put your println inside go();
like
public class example{

int a;
public static void main(String[] args) {

go();
}

go(){
System.out.println(a);
}
}
its the main that causes the ball to roll, no main nothing happens.

regards
vinay.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vinay Thippeswamy:
no main nothing happens.


FALSE! Classloading happens. This code prints "Hello, World" and I don't see no main!
 
Vinay Thippeswamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HelloWorld prints but you get an exception if you do that.


C:\pj>java MyClass
Hello, world!
Exception in thread "main" java.lang.NoSuchMethodError: main

thats not good OO either
[ June 07, 2006: Message edited by: Vinay Thippeswamy ]
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then try this
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vidya sagar:
Then try this

Excellent!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhinaba Basu:
Yes it is static initializer. But dr we can call methods like, ...


No, that is not a static initializer in Jeff's example. It is an instance initializer - that's not the same as a static initializer.
[ June 08, 2006: Message edited by: Jesper Young ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to the original question: "Why can't you put executable code outside of a method (or initialiazation block) but still inside a class?"

Short answer: Because the people that designed the Java language said so.

Long answer: I'm not sure exactly, but it probably has to do with the fact that a lot of Java syntax comes from C++. However, I think the best way to explore this option is the questions that Jesper asked above: What would it mean if you COULD do this?

Layne
[ June 08, 2006: Message edited by: Layne Lund ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!