r/javahelp • • Aug 31 '22

Unsolved Java Static

Why cannot we use System.out.println(), outside <main()> or any static function ?

0 Upvotes

7 comments sorted by

•

u/AutoModerator Aug 31 '22

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

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

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

9

u/8igg7e5 Aug 31 '22

You can invoke System.out.println() anywhere that you can use a non value returning statement, including in non-static methods, constructors appropriately typed lambda bodies or method references.

Can you explain why you think this is not the case?

1

u/kichiDsimp Sep 04 '22

If I use it withour curly braces, then it gives error If I use it with curly braces but not writing nothing before the braces, it does not give error but it does not also print

1

u/8igg7e5 Sep 04 '22

Okay. So what you're referring to is not System.out.println() specific. You're not allowed any statements directly in the class body (or outside of one).

By using the braces you're creating an 'instance initialisation block' - in which you can place statements, including instance expressions (since 'this' is defined).

This class shows some of the places you can and cannot use a statement.

// Can't be here... everything has to be insides a type (class/interface/enum/record)
// System.out.println(); 

class Foo {
    // Can't be here... Only declarations (fields, methods and blocks) are allowed here.
    // System.out.println(); 

    static {
        // Inside a static initialisation block
        System.out.println();
    }

    {
        // Inside an instance initialisation block
        System.out.println();
    }

    // This is using System.out.println() as a 'method reference' (used here in field initialisation)
    Runnable methodReference = System.out::println;

    // Inside a expression-form lambda - though of type void (used here in field initialisation)
    Runnable expressionLambda = () -> System.out.println();

    // Inside a statement-form lambda (used here in field initialisation)
    Runnable statementLambda = () -> {
        System.out.println();
    };

    Foo() {
        // Inside a constructor is fine
        System.out.println();
    }

    void someMethod() {
        // Inside a method is fine
        System.out.println();
    }

    // You can't invoke this with 'm(System.out.println())' - you can never use a void-type expression where a value is expected
    // `Void` is actually a meta-type that can't be instantiated...so this method is uncallable (outside of some significant rule-breaking).
    void m(Void v) {
    }
}

2

u/Chemical-Asparagus58 Sep 01 '22

You can use System.out.println() in all methods, maybe you had a typo or didn't add a semicolon at the end when you wrote it into your code so it didn't work.