• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Very basic but tough question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Java in General (beginner).
 
Abhinaba Basu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can do that, just get the syntax right:
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then try this
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vidya sagar:
Then try this


Excellent!
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic