Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object creation

 
Saumyaraj Zala
Ranch Hand
Posts: 56
Android Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic