• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Private Constructor

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





Can anybody explain me this program:

The output::: no output.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the reason there's no output is simple. There's only one method that ever produces any output - go(). And you never call it.
 
priyadharshini vijayaraghavan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why can't we call the go1() even though having the refernce c1 to it.

I didnt understand the concept properly. Can you explain it to me?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have called go1(). go1() doesn't output anything. It returns a value - that's not the same as output.

go() produces output, but that isn't called. See the difference between the two methods?
 
priyadharshini vijayaraghavan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi matthew, thanks for explaining me, but I understood partially. could you explain a bit further.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's try this. Why do you think there should be some output? Which lines of the code do you expect to produce output?

- You creating a new Constructor object.

- You then call go1(). This returns the String "this is from go1". You don't do anything with this String, because you don't assign the value returned to anything.

- The program ends. Nothing else happens.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want to see an object in output console, then you need to pass the object into the program output device[which show the object in console/output]. for example pass the returned obejct into System.out.println method.
System.out.println("object need to visible");
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to find out why the go1 method doesn't return anything see the complete modified version below:




 
priyadharshini vijayaraghavan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all, yup I understood. Thanks for explaining.

Thanks all . I was just looking at the static method and private constructor and was wondering about the output.

Still have a long way to finish learning for the exam..

reply
    Bookmark Topic Watch Topic
  • New Topic