aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object creation" Watch "object creation" New topic
Author

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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:


Olly
Richa Sinha
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
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: http://aspose.com/file-tools
 
subject: object creation