how to print a message to console without using main()
function?(do not use even static blocks also.)




Answers were Sorted based on User's Feedback



Answer / cvs

class testing{
public testing(){
System.out.println("Hello ");
}
}
public class TestClass{
static testing t = new testing();
public TestClass(){
}
}

Is This Answer Correct ?    29 Yes 18 No

Answer / s.j

in c language
#define main begin
void begin()
{
printf("HELLO");
}

Is This Answer Correct ?    8 Yes 3 No

Answer / shanu zabeen

class testing
{
public testing()
{
System.out.println("world");
}
}

public class Demo
{
static testing t = new testing();
public Demo()
{
}
}

Is This Answer Correct ?    4 Yes 2 No




Answer / harsha

#include<stdio.h>
#define func main
int func()
{
printf("Virat
");
}

Is This Answer Correct ?    0 Yes 0 No

Answer / s.j

in c language
#define begin main();
void begin
{
printf("HELLO");
}

Is This Answer Correct ?    0 Yes 1 No

Answer / vijay nag

use a constructor and declare a global object.

class message
{

public:
message("Hello world\n");
}a;
void main(){}

Is This Answer Correct ?    18 Yes 20 No

Answer / jaya

Sorry. Missing return statement in the former post

public class classWithoutMain {
private static int x = load();
private static int load() {
System.err.println("This message will be printed");
System.exit(0);
return 0;
}
}

Is This Answer Correct ?    4 Yes 7 No

Answer / amit dubey

class testprint
{
static String print=testprint.toString("test");
public static String toString(String s)
{
System.out.println("I am Here");
return s;
}
}

Is This Answer Correct ?    6 Yes 10 No

Answer / gaurav

with use of main we can print message in cosole by the use
of static block because java provide class loading machanism
i n program ,
Example-
class A
{
static
{
System.out.println("execute with out use of main");
System.exit(0);
}
}

Is This Answer Correct ?    1 Yes 5 No

Answer / jaya

public class classWithoutMain {
private static int x = load();
private static int load() {
System.err.println("This message will be printed");
System.exit(0);

}
}

Is This Answer Correct ?    2 Yes 7 No

Post New Answer



More J2SE Code Interview Questions

how to create a (*)pyramid using java codes???

5 Answers   Infosys,


write a program that will ask the user to enter a number n and display the product of all numbers from 1 to n.

1 Answers  


inner join,outerjoin,trigger,stored procedure explain with code snippets?

1 Answers   IBM,


how to print a message to console without using main() function?(do not use even static blocks also.)

12 Answers   Google, Zoho,


For printing a message we use System.out.println in normal programs. We use String msg="text....."; Can't we use String msg=" " in normal programs and System.out.println("........") in applets. Please answer this question?

0 Answers  






write a program in java to find the moving average of all prime numbers between 2 and 100.

0 Answers  


How to create Date method to set the date in Ms Access

0 Answers  


write a program in java to solve a system of n-variabled simultaneous equations using the guassian elimination method. let the maximum possible value of n be 100. run the program using hypothetical values for a set of 10- variables simultaneous equations. print out the program, the input equation and the results generated by the program.

0 Answers   TCS,


what are the other loops except for for,while,do while and until?

0 Answers   Aricent,


Why the program getting error if we don't use String args[] in main(), even in the case of not getting any arguments from command line?

1 Answers  


is public static void main() work in java?is "String arg[]" needed as argument?

4 Answers   Tech Mahindra,


does anyone know the code to display a triangle using a applet?

1 Answers  






Categories