-1
class a { 
public static void main(String args[]) {

 }
}

i want to print "hello world" without touch or use above code and also without use of static and final keyword......

kosa
  • 65,990
  • 13
  • 130
  • 167
ajay panchal
  • 145
  • 1
  • 11
  • 1
    is this a code challenge? If not.. why? If yes, offtopic – WorldSEnder Jul 07 '15 at 18:12
  • 3
    @Nambari Though this question should be closed, I don't see it as a duplicate of that. – tnw Jul 07 '15 at 18:14
  • @tnw: Feel free to reopen vote. Solution for this question is the question I tagged as duplicate, which is why I tagged as duplicate. – kosa Jul 07 '15 at 18:15
  • @nambari he asked _without_ static keyword. It isn't a duplicate. – Olivier Poulin Jul 07 '15 at 18:15
  • 2
    No, it isn't. This question explicitly asks for a solution without `stati`c. The duplicate linked very obviously uses `static`. The solution there does NOT answer the question at all given the requirements and therefore is not a duplicate. – tnw Jul 07 '15 at 18:16
  • 1
    In my opinion OP confused about all these terms. Feel free to vote for re-open if you think it is not duplicate and there might be an answer. – kosa Jul 07 '15 at 18:17
  • 2
    This question does not have an answer, but it isnt a duplicate. It should be closed. – Olivier Poulin Jul 07 '15 at 18:19
  • I voted to reopen. Again, not that I think this is a good question anyway (I think "too broad" applies much better here) but that's a really unfair assumption. – tnw Jul 07 '15 at 18:19
  • hey Nambari in your questions answer u can use static key word but i want that type of solution in which the static and final key word not used.... – ajay panchal Jul 07 '15 at 18:21
  • 2
    @OP which version of Java we are talking about? Also what do you mean by without `static` or `final`? Also bonus question: where does that limitation come from? – Pshemo Jul 07 '15 at 18:21
  • @Nambari See above response by OP, I'd ask that you please remove your close vote now that it's stated explicitly (twice, now) that OP does not want to use the static key word and the marked duplicate obviously does not apply. – tnw Jul 07 '15 at 18:25
  • Ok done. Sorry for the inconvenience! – kosa Jul 07 '15 at 18:26
  • 1
    @ajaypanchal: One more bonus question is, what do you mean by"without touch or use above code" – kosa Jul 07 '15 at 18:28
  • it means the above code is fixed, you can't modify the above code but yes u can write anything to print "hello world" above or belove this code – ajay panchal Jul 07 '15 at 18:38
  • What is the *exact* part that cannot be modified? Can I change class into enum (see my answer)? Can I add methods, fields, initializers (see my answer again)? – YoYo Jul 07 '15 at 18:55

3 Answers3

4

What are the different ways to initiate code execution.

  1. Use public static void main(String args[]) { ... }
  2. Use your static class initializer static { ... }. Problem: You need to get the class to load first. Solution: attempt to start the main method, but that means you need to have at least an empty main. However - OP states we cannot use 'static'.
  3. Use an instance initializer { ... }. Problem: How do you instantiate an instance. Solution: initialize a static field with a class instance. However - OP states we cannot use static fields.

So what is left. I assume that we can use the empty main posted by the OP. From there on we build up a 4th solution:

public enum Play {
  PLAY;
  { System.out.println("Hello World!"); }
  public static void main(String args[]) {  }
}

This should fulfill the conditions:

  1. we do not modify the static main method, although we need the empty body just to get the class loaded.
  2. we do not use a static initializer, we use an instance initializer instead.
  3. to instantiate, and get the instance initializer started, we use an enum. Enum's start the initializer for each instance, meaning for each of the enum constants.

Also note the output when you try to execute a class without a main:

$ java -classpath . Play
Error: Main method not found in class Play, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

So that leaves us also the clue that we potentially can use javafx.application.Application. I did not investigate this trail any further.

YoYo
  • 9,157
  • 8
  • 57
  • 74
  • Nice finding! +1. I liked the way you explored different options. By the way you changed OP class signature to enum, which I am not sure OP interested to take up or not. – kosa Jul 07 '15 at 18:53
  • @Nambari Yes - I need clarification - see comment to the OP's question. – YoYo Jul 07 '15 at 18:57
  • My guess is OP doesn't know what he is talking, he just got this question from some where and posted here. In my opinion answer would be either this (or) using static { } blocks hack in java versions 6 or below. – kosa Jul 07 '15 at 18:58
2

You can create a JavaFX application, which is usually for displaying a GUI, and use it to output hello, world to the console and immediately shut the JavaFX GUI system down. JavaFX applications are executed by a special launch path in the java launcher. JavaFX applications do not need a main, static and final keywords.

The following snippet is just for demo purposes, I wouldn't recommend writing a JavaFX application unless you actually wish to display a GUI.

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;

public class PlayApp extends Application {
    @Override
    public void start(Stage stage) {
        System.out.println("hello, world");
        Platform.exit();
    }
}

// Totally useless code from the original question 
// goes below the real app code and is just ignored
// and never used when the application runs.
class a { 
public static void main(String args[]) {

 }
}

You need to use Oracle Java 8+ to compile and execute the JavaFX application as below:

javac PlayApp.java
java PlayApp

The a class is in the PlayApp.java file only to meet the original poster's requirement from a comment:

it means the above code is fixed, you can't modify the above code but yes u can write anything to print "hello world" above or belove this code


Really, if you are writing a console application, just use a standard app format (with a static main method) instead:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

Unrelated advice on asking questions

In future, if you post such questions, I suggest that you also supply the reason for the question and why the bizarre constraints are placed on solutions. Also, address concerns such as WorldEnder's first comment on your question: "is this a code challenge? If not.. why?"

jewelsea
  • 150,031
  • 14
  • 366
  • 406
0

You can't just print "hello world" without MAIN method and also without using STATIC and FINAL key word in java because the main method is a function required you don't have to apply any rocket science in that thing if u are using intelliJ IDEA or any other IDE anyways this is the code

public class Basics_Of_Java {
    public static void main(String[] args) {
        System.out.println("Hello world");

basics of java in 1st line is the name of the java file you may have another name.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Siddhant
  • 1
  • 1