11

In an interview I have asked this question:Without using static and main how could we print message on console?Is it possible?

shree18
  • 1,279
  • 1
  • 11
  • 15
  • 6
    possible duplicate of [Printing message on Console without using main() method](http://stackoverflow.com/questions/8605137/printing-message-on-console-without-using-main-method) – Dusan Plavak Aug 29 '13 at 08:23
  • @Raza but where does the sysout call go, if not in main or a static block? – vikingsteve Aug 29 '13 at 08:30
  • 1
    try this link:[printing message in java without main method/static block](http://stackoverflow.com/questions/18171465/can-we-print-a-java-message-on-console-without-using-main-method-static-variabl). The solution mentioned still uses main() function to call the **"initializer block"**. – r3ap3r Aug 29 '13 at 08:31
  • 1
    @Dusan not exactly a duplicate because here the keyword 'static' is forbidden. – Arnaud Aug 29 '13 at 08:47
  • @r3ap3r Hey that link worked for me. – shree18 Aug 29 '13 at 08:47

8 Answers8

29

You could define a custom class loader that prints your message :

public class MyClassLoader extends ClassLoader {
    public MyClassLoader(ClassLoader other) {
         super(other);
         System.out.println("Hi there");
         System.exit(0);
    }
}

Then run the java command :

java -Djava.system.class.loader=MyClassLoader

(don't need to add a class as parameter)

Arnaud
  • 7,259
  • 10
  • 50
  • 71
  • @Chafousin Its working !Can you explain me What stands for java -Djava.system.class.loader – shree18 Aug 29 '13 at 09:39
  • 2
    `-Djava.system.class.loader` is used to define the JVM class loader, i.e. the object that is responsible for loading the classes. Here we provide our own class loader and we "hack" it so that it displays some text, then quits. – Arnaud Aug 29 '13 at 09:42
2
I have asked this question:Without using static and main how could we print
message on console?Is it possible?

Answer is No!

You cannot execute anything unless main() method is called. Prior to Java 7 classes were loaded before main() method was looked up. So you could print your data through static blocks(static block gets executed when classes are loaded) but from java 7 onward even that is not possible. So you always have to execute the main() method first.

Even in frameworks like Spring beans are generally initialized only when it's context is referenced(again main() is required to be executed first).So there is no way you can print something to console without invoking main() method or through static functions/blocks.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • @ Aniket Thakur you are right.But my question was for main and static blocks.So below code works for me. private static int i = m1(); public static int m1(){ System.out.println("Test...!!!!"); System.exit(0); return 0; } – shree18 Aug 29 '13 at 09:02
  • @shree18 As I mention earlier your code will work only prior to Java 7 because classes were loaded before main() method lookup. As for as your question is considered(taking into consideration interview perspective) if you answer yes with your code that will only show you are not aware of recent java changes. I must not say recent :P (java 7 has now been for some time) but i hope you get my point. – Aniket Thakur Aug 29 '13 at 09:07
0

Java is a OOP language.

You can not create a program without creating a class and adding it a static main function.

Then, you can call System.out.println to print a line.

So, the answer is no.

blue112
  • 52,634
  • 3
  • 45
  • 54
0

You always need to put syso in a block of code, perhaps:

public class example { public void message(){ System.out.println("Hello"); } }

Here the method is not static

Savins
  • 81
  • 1
  • 13
  • @Savins The solution you provided requires a main() method to call the function **"message"** – r3ap3r Aug 29 '13 at 08:36
  • @r3ap3r I'm not sure, but I do not necessarily mind need an airplane, for example if the message you use to display the value of an object while depuras, in tests, no main methods. It is true that this is a very specific case. – Savins Aug 29 '13 at 08:54
0
public class Test {
    public static PrintStream ps = System.out.printf("%s", "hello");
}

Pretty strange question for interview. It will print hello, and throw Exception in thread "main" java.lang.NoSuchMethodException.

Mikhail
  • 4,175
  • 15
  • 31
  • Not in/after Java7!!! I tried your code and I got **Error: Main method not found in class Test, please define the main method as: public static void main(String[] args)** – Aniket Thakur Aug 29 '13 at 08:49
  • Well, on 6 java it works. It`s true that on 7th code is not executed without main method. But still it has static variable. I dont know whether its acceptable. – Mikhail Aug 29 '13 at 08:55
  • As I said it is kind of bug which is fixed in Java 7. It works on all JDK prior to 7 because main() method was looked up after loading classes. – Aniket Thakur Aug 29 '13 at 08:58
0

The answer is definitely No.

At least either you need a static block or you need an empty main().

See the below examples:

1.

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

OUTPUT:

hai

2.

public final class ABC {
    static{
             System.out.println("hai");
          }
}

It will print 'hai' while running but after that one exception will also occur.

OUTPUT:

java.lang.NoSuchMethodError: main

hai

Exception in thread "main"

  • Because you said there's no way without main or static block, but there is. See the answer to this question. – schlingel Sep 03 '13 at 08:23
0

possible with static block which executes before main method

rams0610
  • 1,041
  • 10
  • 8
0

@ Aniket Thakur As i am not able to comment because of my points. but following programs prints the message before it enters main. NOTE: I used Java 7 and Java 8. both works fine and runs static block before main.

public class PrintBeforeMain {

    private static int i = m1(); 

    public static int m1(){ 
        System.out.println("m1(): Before main() through static method..."); 
        return 0; 
    }

    static{
        System.out.println("Inside standalone static{} block");
    }

    public static void main(String[] args) {
        System.out.println("Inside main()");
    }
}
josliber
  • 43,891
  • 12
  • 98
  • 133
Zeitnot
  • 33
  • 5