This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes object creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object creation" Watch "object creation" New topic

object creation

Saumyaraj Zala
Ranch Hand

Joined: Aug 06, 2012
Posts: 56

Can i create an object outside a method??? Can i also access variables and methods outside an method??? It give error...please help with this method stuff... am not getting what to write inside and outside the method...

Why do we fall? So that we can learn to pick ourselves up.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33134

Yes. Show the code an the error. Maybe you aren't doing it right.

Also, I changed your subject for you. All caps is considered shouting on the internet.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 970
Actually no "rogue" pieces of executable code are possible.
You can have (unnamed) initializing blocks and assignments but nothing much more.

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Ivan Jozsef Balazs wrote:Actually no "rogue" pieces of executable code are possible.
You can have (unnamed) initializing blocks and assignments but nothing much more.

Yes exactly, technically speaking you cannot write anything outside of the method other than declaration. So everything that you put outside of a function is simply for initialization. It's something that's hidden by the compiler so it can be quite confusing

Think of it in this way:

For dynamic field (instance variables):
is the same as:

For static fields:
is the same as:

Richa Sinha

Joined: Sep 14, 2012
Posts: 5
Yes, you can declare variables outside a method in java. Also, you can create an object and acess variables and methods outside a class. The following example illustrates the same. Suppose you have a class Test and class Sample as follows:

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46437
Please always use the code button in your posts. I have edited your post to show how much better it looks, and also corrected the spelling mistake: is should be class not Class.
What you are doing is declaring those fields. Field declarations must be outside methods, otherwise they would turn into local variables. You are allowed to combine a declaration with an initialisation in one line. Unfortunately a class which supports such initialisation may suffer from poor design, e.g. non‑private fields.
What you must not do outside methods (or initialiser blocks) is to write statements. It’s all in the Java Language Specification, which is really easy to read.

Challenge: find the one lie in this post
I agree. Here's the link:
subject: object creation
It's not a secret anymore!