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

From the above, if we say that MyClass is the class and public static void main(String[] args) is the method, then I would like to know what part of the code would be considered as the Object.

If we say that classes have objects and objects have methods, then in the code above, which part is the object? Is there any object created here?

2
  • 1
    Short answer; an object is an instance of a class Commented Jun 10, 2013 at 5:47
  • 1
    @RaduMurzea actually it's harder to explain for a starter...so less answers Commented Jun 10, 2013 at 5:58

5 Answers 5

9

static methods don't belong to object references but to classes instead. You can execute a static method without creating an instance of the class. Knowing this, the only object references that would be created here would be:

  • String[] args object
  • Every String inside args array
  • "Hello world" String.

Note that args and its contents aren't created by you, the JVM will do it (thanks to Thilo's comment).

Sign up to request clarification or add additional context in comments.

10 Comments

And those Strings are not created by your code, either (the JVM launcher does that automatically).
There are only two objects definitely created here - "Hello World!" and the array args.
Actually, the String literal "Hello World" can be said to be created by the main method (even though that is not strictly true with the ways strings work either)
+1 and.. Dont know if this will help of confuse more, but out is a PrintStream object :P
Also arg contents will be created only if arguments are sent to main mehod while executing the program. Else only 1 and 3 are applicable as per OP.
|
4

In above case no object of your class is created. To do that you need to write:

MyClass myclass = new MyClass();

Comments

0

No object created there.Because that is a main method where the program starts. If it is not a static,otherwise, it would need an instance of the object to be executed.

1 Comment

What about "Hello world" String?
0

Static Method no need to create object for execution. When you load the class the static members on the class are loaded automatically by JVM.

Specially for execution in Java the JVM find the main method and execute it from the beginning of execution.

Then, each statement from the main method is executed until the termination point.

In the main method what is String args[] it is an array of objects, which is useful when you need to pass the parameters via command line.

So, finally static method execute on when the class is loaded on JVM. So we don't need instance/object for the main method execution

So when is the main method is loaded on JVM? It's loaded when you initialize to create any objects like this

MyClass myobj = new MyClass();

It calls the default constructor MyClass() and load the class

Comments

0

In the code you provided, there isn't any object created explicitly. The 'MyClass' is just a class definition, and the 'main' method is the one that gets executed when you run the program.

In object-oriented programming, objects are instances of classes. They have variables (attributes) and methods (behaviors). However, in this code snippet, there's no object created or used inside the 'main' method.

If you want to create an object of the 'MyClass' class, you can do that by using the 'new' keyword like this:

MyClass myObject = new MyClass();

This line of code creates a new object of the 'MyClass' type and stores it in the 'myObject' variable. After creating the object, you can access its methods and variables using the dot notation, like 'myObject.someMethod()'.

enter code here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.