• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Printing "Hi Hello Bye!!"

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In one of the interview I have to face the below question.

There is only one print statement in main function. i.e. System.out.println("Hello"); without altering/ adding new statements to the main function get me an output of "Hi Hello Bye!".

I wrote the below code snippet

public class Test1{
static{
System.out.println("Hi");
}

public static void main(String [] args){
System.out.println("Hello"); //no extra statements here
}
}

The above code successfully producing the output "Hi Hello" since I knew that once we enter to a class irrespective of creating object for that class, static block will get executed. But how to print "Bye!!" at last? Please some one help me to solve this problem.

Thanks in advance.
 
author
Posts: 23943
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

One option you can try is using a shutdown hook -- code to be executed upon JVM shutdown. Take a look at the Runtime class for more info.

Henry
 
Henry Wong
author
Posts: 23943
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BTW, not really a topic about jobs -- moving to the java in general forum.

Henry
 
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can extend the class and call main method .

That will solve you problem

Regards
Raza
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could pass command line arguments...
 
Gangadhar Shetty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raza Mohd wrote:you can extend the class and call main method .

That will solve you problem

Regards
Raza



Can you please elaborate it? Because even if I extend the class where can I put the print statement to print "Bye!" and where can I create object for the derived class? since the main requirement is 'nothing should be added to Main block'
 
Gangadhar Shetty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jothi Shankar Kumar wrote:You could pass command line arguments...


I have already tried this option but couldn't prove it. Once you send command line arguments, it will be stored in 'args' array. Since this is local for the 'main' function, then how will you call this array out of 'main' block which will be out of scope.

Can you please elaborate your idea and correct me if my comments are wrong.
 
author and iconoclast
Posts: 24206
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raza Mohd's answer would require that you invoke a different class from the command line, not this one, which I think is against the spirit of the question. Jothi Shankar Kumar's answer isn't an option. But Henry's answer is a good one, and likely the one the interviewer had in mind.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(args[0]);

Run it like java YourClass "Hi Hallo Bye"
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Raza Mohd's answer would require that you invoke a different class from the command line, not this one, which I think is against the spirit of the question. Jothi Shankar Kumar's answer isn't an option. But Henry's answer is a good one, and likely the one the interviewer had in mind.



Why isn't it an option?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24206
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll have to show us some code to explain your idea.
 
Ranch Hand
Posts: 597
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
One option you can try is using a shutdown hook -- code to be executed upon JVM shutdown. Take a look at the Runtime class for more info.

Henry



This is what Henry mean

[ EFH: Code deleted -- JavaRanch is NotACodeMill ]
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
One option you can try is using a shutdown hook -- code to be executed upon JVM shutdown. Take a look at the Runtime class for more info.

Henry



Or you can do this.


or you can add a finalize() method and say "bye" in that. but then you will have to create a object and point it to null then call the Garbage collector, plus there is no guarantee that GC will be called in time.

If you feel like impressing the interviewers , then try using the Runtime.exec() methods to call the host operating system's echo command , just for fun.
 
Rohan kanade
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jothi Shankar Kumar wrote:

Ernest Friedman-Hill wrote:Raza Mohd's answer would require that you invoke a different class from the command line, not this one, which I think is against the spirit of the question. Jothi Shankar Kumar's answer isn't an option. But Henry's answer is a good one, and likely the one the interviewer had in mind.



Why isn't it an option?



because the constraint is that you cannot add anything to the main method, and how is one supposed to get the arguments passed to the main method without adding anything to the main method?
 
Gangadhar Shetty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan kanade wrote:

Henry Wong wrote:
One option you can try is using a shutdown hook -- code to be executed upon JVM shutdown. Take a look at the Runtime class for more info.

Henry



Or you can do this.


or you can add a finalize() method and say "bye" in that. but then you will have to create a object and point it to null then call the Garbage collector, plus there is no guarantee that GC will be called in time.

If you feel like impressing the interviewers , then try using the Runtime.exec() methods to call the host operating system's echo command , just for fun.



Thanks Rohan the code snippet is working fine.
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic