Print Hellow World without modifying Main method java [duplicate] - java

This question already has answers here:
How can you run a Java program without main method? [duplicate]
(4 answers)
Closed 7 years ago.
here is code
public class Test {
public static void main(String[] args){
System.out.println("Hello");
}
}
And my question is that how to print Hello World without touching main method?
I tried using static block but then Output comes like
World
Hello
But i want Hello World in same line.

Try this
public class Test {
static {
System.out.println("Hello world");
System.out.close();
}
public static void main(String[] args){
System.out.println("Hello");
}
}

Related

why this class not compiling? [duplicate]

This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
compilation error: identifier expected
(4 answers)
Closed 5 years ago.
Why is this error coming? Please see the following code.
class Test{
Hello h=new Hello();
}
class Hello{
int a=10;
System.out.println(a); // error identifier expected
}
Create Class With Same Package
public class Hello {
public void print(){
int a = 10;
System.out.println("Number is :" +a);
}
}
Crate Class to Set the Main Method Within the Same Package of Hello Method
public class Main {
public static void main(String args[]){
Hello h1 = new Hello();
h1.print();
}
}

"Error: Main method not found in class nissan.lakshmi, please define the main method as:public static void main(String[] args) [duplicate]

This question already has answers here:
Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args) [duplicate]
(5 answers)
Closed 6 years ago.
This is my code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class lakshmi {
static WebDriver driver = new FirefoxDriver();
public static void main() {
tc1();
}
public static void tc1() {
driver.get("www.google.com");
}
}
I get this error message:
Error: Main method not found in class nissan.lakshmi, please define the main method as:public static void main(String[] args)
The problem is that Java is searching for the method: public static void main(String[] args) and you only have:
public static void main() {
tc1();
}
Which is not the same. So you need to add the parameter String[] args.
This parameter is a list of all the parameters that were passed in when the java application was called.
Update:
After adding that now it is saying A java Exception has occured
Chances are that there is an issue with the driver or the project setup, make sure the driver is properly imported. If the problem persists do this

How to access a method from the main function in java [duplicate]

This question already has answers here:
Calling Non-Static Method In Static Method In Java [duplicate]
(14 answers)
Closed 6 years ago.
Here's my code:
public class Wallgame {
public static void main(String[] args) {
new Hello();
}
public void Hello() {
System.out.println("Hello!");
}
}
This does not work, and I don't know why I can't access the Hello method. Thanks!
Hello method should be static and you call it by writing Hello(); in the main method.

a function is required to be static in java [duplicate]

This question already has answers here:
cannot make a static reference to a non static method
(5 answers)
Closed 7 years ago.
The following block has an error. It requires the createArrayList function to be static. I cannot understand the reason. I appreciate if anyone can explains that to me in an understandable way.
import java.util.ArrayList;
public class Ceasefire {
public static void main(String[] args)
{
createArrayList();
System.exit(0);
}
public void createArrayList()
{
ArrayList<String> aL1 = new ArrayList<String>();
aL1.add("Item1");
aL1.add("Item2");
aL1.add("Item3");
System.out.println(aL1);
}
}
You cannot call a non-static (createArrayList) method from a static one (main). A static method can only call other static methods, but no instance methods.

Use of Static in the example [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Closed 8 years ago.
I want to know the meaning of static in my file. Everytime, I come across an error and then find out that the word static is missed. Can you please explain when and where should the word static be used.
Here, for example in the code, even if I remove static from the variable, it shows error.
Is, it static class, so it has to be static variable ??
public class TestJavaServer {
static String xCordinate;
public static void main(String[] args)
{
readFromFile();
}
public static void readFromFile() throws IOException
{
xCordinate ="something";
}
A static field is the same in all instances of the class.
Do
TestJavaServer.xCordinate ="something";
and Bob's your uncle.
However, rewrite your code like this instead:
public class TestJavaServer
{
String xCordinate;
public static void main(String[] args)
{
TestJavaServer testJavaServer = new TestJavaServer();
testJavaServer.readFromFile();
}
public void readFromFile()
{
xCordinate ="something";
}
}

Categories