• Post Reply Bookmark Topic Watch Topic
  • New Topic

can object only be created in classes with a "main"?  RSS feed

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whether objects can only be created in the class which has a " main " method or they can be created in any classes
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main method is just an entry point/place for JVM. object can be created anywhere with in any classes. both are irrelevant!
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is NOTHING special about main. For all intents and purposes, it behaves exactly like any other method you would write.

The ONLY thing different about it is that when you type "java <classname>", the JVM looks for the "public static void main(String [])" method in the class you named, and calls/runs it.

It behaves the same, must follow the same rules, and can be treated the same as any other method.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:There is NOTHING special about main. For all intents and purposes, it behaves exactly like any other method you would write.

The ONLY thing different about it is that when you type "java <classname>", the JVM looks for the "public static void main(String [])" method in the class you named, and calls/runs it.

It behaves the same, must follow the same rules, and can be treated the same as any other method.

then whats wrong with this code ?




it is giving me an error at compile time .
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the error you receive? can a method be written inside another method like below?


 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:what is the error you receive? can a method be written inside another method like below?




i tried by putting method outside the main
then first.java is compilling but second.java throws an error


Untitled.png
[Thumbnail for Untitled.png]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is not valid. You can't have executable code that is not in a method.

Why don't you tell us what you are trying to do?

One more point...it would help GREATLY if you would post the text of your errors when you compile. Just saying "I get an error" doesn't help. Posting a .png screencap is also more trouble than it's worth. You can easily cut-n-past directly from the command window into your posts here.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote:

it is giving me an error at compile time .

well, you can declare things directly in class body. i.e, outside of a method . but you cant perform an action/fuction like System.out.println and f.print outside of a method.
action must be inside a method. one more additional example:
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
This is not valid. You can't have executable code that is not in a method.

Why don't you tell us what you are trying to do?

One more point...it would help GREATLY if you would post the text of your errors when you compile. Just saying "I get an error" doesn't help. Posting a .png screencap is also more trouble than it's worth. You can easily cut-n-past directly from the command window into your posts here.

thanks now i got it !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!