• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Doubt with creating objects outside of a method

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that object is created by first declaration and then initialization.
But all of these objects are created inside of a method.
When I try to create an object inside a class but outside of a method then it gives error.
So, why aren't we allowed to create objects outside of a method? Also, does creating object inside of a methods has any advantages?

Any help will be appreciated.
 
Marshal
Posts: 25197
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amol Araragi wrote:When I try to create an object inside a class but outside of a method then it gives error.



Then you're doing something wrong.

So, why aren't we allowed to create objects outside of a method?



You are allowed to create objects outside of a method. This class should compile in your Java:


 
Marshal
Posts: 68069
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amol Araragi wrote:I know that object is created by first declaration and then initialization. . . .

No, that is how you create a non‑null reference. There are several ways to create objects, but the new operator is a frequently used technique. String literals are objects in their own right, so a String literal and String concatenation are also ways to create objects.
What you may get wrong is an assignment statement:-Line 2 is an assignment statement and you are only allowed statements inside methods, constructors or initialisers. That is probably why you can't get your code to compile.
It is useful to declare and initialise a field in one line if you always want to have the same value for that field for all instances of the class. It is better to initialise static fields like that than to try initialising them in the constructor. I shall leave it to you to find out why.
 
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Amol Araragi wrote:When I try to create an object inside a class but outside of a method then it gives error.


Could you post an example of code that demonstrates this?  You should be able to do this:
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!