File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "member variable in constructor doubt" Watch "member variable in constructor doubt" New topic

member variable in constructor doubt

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 481

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.


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

Joined: Jul 20, 2006
Posts: 107
Forward Referencing
I agree. Here's the link:
subject: member variable in constructor doubt
It's not a secret anymore!