Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes concept of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "concept of objects" Watch "concept of objects" New topic
Author

concept of objects

bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32



output=main initializer
but when i dont create object output=main ??? how
can anyone explain
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Object Creation
Initialization Blocks

-hth


OCA7
bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32

i have read that link , as here there is no constuctor ,so a default constructor get created on creation of object which prints that initializer

Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
bhavneet kaur wrote:
but when i dont create object output=main ??? how

You've answered your own question.
You don't create an object therefore the object initialiser is not executed.
bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32

it means whenever default costructor created it reads the whole program
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
bhavneet kaur wrote:
it means whenever default costructor created it reads the whole program

No. It means that initialization blocks are executed only when something is instantiated.
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 175
There is a bit more to initializer blocks, just to clarify them:

Say for instance we have classes A and B

Outputs:
A Object initializer
A Object initializer
B Object initializer

Because of the calls to 'super' when we instantaite a subclass.


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32

A object initializer executed two times because of inheritance concept ,right?

and secondly to say in one line initialization blocks are those that do not have main method ,suppose there are three classes in a program and one class would contain main so rest of the classes are initalization blocks??
bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32
and also that are outside main
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 175
bhavneet kaur wrote:A object initializer executed two times because of inheritance concept ,right?

Indeed

bhavneet kaur wrote: and secondly to say in one line initialization blocks are those that do not have main method ,suppose there are three classes in a program and one class would contain main so rest of the classes are initalization blocks??


1) Initializer blocks can be static or non-static and are easy to spot as they have no name, just braces {} or static {}.
2) The main() method doesn't have to appear in your class at all. It is there so you can invoke other code from it, either using a command-line or some IDE to get things running.
3) If you have more than one class in your program, then generally you have a top-level class (nothing to do with main() method) and the other classes are some kind of nested class, but thats another topic

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: concept of objects
 
Similar Threads
initialiser block/constructor
Why it gives ERROR without 'this'
class question
why get's printed
tricky program