Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes member variable in constructor doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "member variable in constructor doubt" Watch "member variable in constructor doubt" New topic
Author

member variable in constructor doubt

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477

In the above program,assigning �s� to �msg� before knowing the member variable �msg� declaration.How come the constructor inside msg=s is working without showing any compile error?when executing Line1,we didn�t know what type msg is?
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Question is not which come first? Important thnig is the flow of controlat runtime.

When you call constructor, first msg is assigned default value. Since its type is String, it is assigned to null. Then after that constructor will explicitly set msg to s. So at that time, runtime system knows the type of msg.

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
Forward Referencing
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: member variable in constructor doubt