Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions Display Hello even before main get executed??

Have Programming Question? Ask it here!
 
 
 


Amar
Display Hello even before main get executed??
3 Answer(s)      3 years and 6 months ago
Posted in : Java Interview Questions


I have a class (main method) as follow.....As I know this can be done using static method, but Q is how?

**class Hello{ public static void main(String[] args){ System.out.println("Darling"); } }**

I want result as Hello! Thank you!! when I run the program.

Please assist???????


Advertisement
View Answers

March 15, 2011 at 6:06 PM


class Hell { static { System.out.println("Hello, Thank You"); }

public static void main(String[] args) { System.out.println("Darling"); }

}

I used here static block try it



March 16, 2011 at 2:28 PM


class Hello{
    static void display(){
        System.out.print("Hello ");
    }
    static void show(){
        System.out.print(", Thank you");
    }
    public static void main(String[] args){
        display();
        System.out.print("India");
        show();
        }
        }


March 17, 2011 at 3:49 PM


Thank you for your prompt and quick reply....

I got the required output with below code.

public class Test1 { public static void main(String[] args) { System.out.print("Darling, "); }

static
    {
    System.out.print("Hello ");
    main(null);
    System.out.println("Thank You! ");
    System.exit(0);
}

}

Output: Hello Darling, Thank you!



Related Tutorials/Questions & Answers:
Advertisements
get
Advertisements
 

 

 

DMCA.com