Java "Hello world" via object with jdoodle

So I’m using this online editor to help with learning Java and I’ve written this. I’m just trying to print “Hello World!” via an object.

 public class test {
    public static void function() {
        System.out.println("Hello world!");
        
    }
}

public class Main {
   
    public static void main(String[] args){
        test t = new test();
        t.function();
    }
}

The error I get is

 /Main.java:11: error: non-static variable this cannot be referenced from a static context
        test t = new test();
                 ^
1 error

How do I fix this?

Its been a while since I worked with Java, so my tips should be taken with a grain of salt.

Class names usually start with a capitol letter, so the class name would be Test rather than test. I don’t remember if Java makes this a rule or it’s just a followed convention.

A static method is a method that exists outside the instance of a class, so essentially its “static” to the class itself, rather than an instance of the class.

public static void main is the most common, as it’s used as the “starting point” for your Java app. So you can have as many Main class instances as you want, but there is only one “main” method. If you want to call this method yourself directly, or any statically defined method you’d not create an instance of the class. So you’d do something like Main.main.

For your Test/test class you wouldn’t want to use new here. Rather just use test.function() or Test.function()

Finally, I’m not sure if the word “function” is special as a method name. Maybe Java has a new feature I don’t know about that leverages function as a special method name? I’m not sure, but maybe changing the name to something else might change the error.

1 Like

Hey, thanks for your response. I got rid of “public” from test and it’s working as expected. Don’t understand why though.

Is it just me, or is this language tougher to learn than most? “public static void” looks nonsensical, I had a much easier time trying to learn C++ than this stuff.

Java is actually one of the top “starter” languages due to being strict about its syntax. A key factor is its verboseness.

Stuff like public static void all has specific meaning.

public means its accessible outside of the class/instance
static means its “specific” to the class, and not aninstance
void is the return value, so this method returns nothing.

Forcing you to be specific about this code, allows IDE’s, the developer, and the compiler all to have basically complete context. Other language’s either don’t provide this or provide less “safe” syntax.

Furthermore Java is an Object Oriented Language, which allows you to model your problem around the idea of classes. This is both a pro and con. Some things are easy to model this way, other’s not so much. Regardless its more to learn.

The other thing about this is some languages are approachable, but actually are harder to use day to day. C and C++ are both “C-like”, just like Java is, so some of their syntax is the same. However, both C and C++ are considered to be one of the hardest languages to use correctly. Most modern security related bugs are actually tied with incorrect use in one of these two languages. Java was designed around issues found with programming in C, stuff like memory allocation, compilation, garbage collection, IDE experience, and scalability are all things Java specifically set out to address.

1 Like