Two Laptop Bag
The moose likes Beginning Java and the fly likes reference id Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "reference id" Watch "reference id" New topic
Author

reference id

abhinav sood
Greenhorn

Joined: Aug 05, 2011
Posts: 9
this program gives the output null and zero can any one explain that.....



[moderator edit: added code tags]
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2207

Welcome to the Ranch, Abhinav !

can any one explain that.....


yes:

First you call:


This will create a new emp() object, call its get() member, and then loses the reference to the object (you don't store the object in a variable).

Then you call:


This will create a brand new object, call its show() method -- that will print null and 0 because you didn't use get() to set its members, and then loses the reference to the object too.



What should you have done:

when creating the object, store it in a variable of type emp (emp myEmp = new ....)
then call the members of that variable (myEmp.get(), myEmp.show() )



Some style remarks:
A class by convention starts with upper case (class Emp)
A method to set properties should be called set..(), not get..().
 
 
subject: reference id
 
Threads others viewed
Why doesn't the subclass variable hide it?
Doubt in String class
permanent memory???
How Elegant Is This Solution? Can You Offer Me Recommendations?
equal(Object o) doubt
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture