KawaiKx
KawaiKx

Reputation: 9920

Where is the object in this Java code

I just started learning OOP using Java. First code I wrote is :

class day1 {
  public static void main(String args[]) {
    System.out.println("Java drives the web");
  }
}

When I compile this source file, I get a .class file. When I interpret it at command line, it gives me the desired result.

My question is: I have not created any object. I have just wrote a class named day1. How am I able to interpret it? OOP is about object drawn from the classes. But I have not drawn anything from the class day1.

I am confused in OOP.

Upvotes: 0

Views: 206

Answers (7)

Bacteria
Bacteria

Reputation: 8596

Object Oriented Programming is a paradigm that provides many concepts such as inheritance, encapsulation, polymorphism etc. surprisingly these three major OOPs concept are present in your small simple Hello World program also. I am trying to prove that, please find below the clarifications

1. Use of Inheritance concept in your program

The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object.

So although you did not extend any class but still your Day1 class implicitly extending Object class and inherits all the methods of Object class. So you already used inheritance concept here.

2. Use of Encapsulation concept in your program

Encapsulation in Java or object oriented programming language is a concept which enforce protecting variables, functions from outside of class, in order to better manage that piece of code and having least impact or no impact on other parts of program due to change in protected code.

Encapsulation can be achieved via access specifiers in Java and you already applied encapsulation concept in your program. You declared your main method with access specifier Public – so that main method can be called by JVM. You made this public to permit call from outside of the application.

3. Use of Polymorphism concept in your program

Polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form

System.out.println("Java drives the web");

Here you used println() method of PrintStream class. If you go to the source code of the PrintStream class, you will find ten methods are present in this class with same name println but with different parameters.In short PrintStream class contains different overloaded method. So you also used Compile time polymorphism (static binding or method overloading) here.

If you are only talking about object, then probably you got your answer from @MadConan

Answer from @MadConan

There are many objects in your code

  • class day1: This is your Class.
  • Class is an object. args[]: This is an object of type array. System:
  • The System object is available everywhere. It's part of the core API.
  • out: This is the PrintWriter object instance which is a member of the System object.
  • "Java drives the web": That is a String object.

Just because you never use the new keyword doesn't mean you aren't using any objects.

Upvotes: 1

wizebin
wizebin

Reputation: 730

The class day1 is the pattern that your main object would be created from- if you were to instantiate it as an object. Which a static method will not do, so in your case the program is still "object oriented" because it is based off of an object pattern. You could access other static methods and variables if you created them in the class, but if you were to create non static methods or variables they couldn't be accessed without an instantiation of your class.

Take a look at this legacy question about why main is static.

Sorry for any misinformation!

(Thanks to jesper for correcting me!)

Upvotes: 2

paxmemento
paxmemento

Reputation: 291

A string object is created when the print method is called.

What you have is a class with a static method, main. Static methods are called on the class, and not on the object. So the program will print the string value, from the main method called on the class.

class Day1 {
private String name;
public String getName() {
 return name;
}
..
}

To call the method getName, an instance of day1 has to be created by calling the new keyword

Day1 o_day1 = new Day1();

Then invoked like

String v_name = o_day1.getName();

What has happened is a default no-args constructor constructs the object when one is not specified, and a reference to the object is returned. The reference you'll use to invoke non-static methods (methods without the static qualifier).

Upvotes: 1

MadConan
MadConan

Reputation: 3767

There are many objects in your code

  • class day1: This is your Class. Class is an object.
  • args[]: This is an object of type array.
  • System: The System object is available everywhere. It's part of the core API.
  • out: This is the PrintWriter object instance which is a member of the System object.
  • "Java drives the web": That is a String object.

Just because you never use the new keyword doesn't mean you aren't using any objects.

Upvotes: 1

Display Name
Display Name

Reputation: 8128

Java is not pure OO language, and also it doesn't require creating objects. You can write arbitrarily complex programs without creating any object — but that would be very hard.

Upvotes: 1

jbarrueta
jbarrueta

Reputation: 5175

In your code you didn't create an Object s day1 class, you loaded day1 class an execute an static method that belong to the class in it.

In you example you don't have an instance field, but if you had one you wouldn't be able to access it.

Also in your example you have one String object "Java drives the web" that you can call instances methods in it and System.out which returns the PrintStream to print your the message.

In order to create an object of day1 you would need to call

day1 myInstance = new day1();

That will be the way to start working with objects.

Hope this helps,

Jose Luis

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32517

and this is about static methods. Static methods and fields are accessible without instance of given class and every object of given class share the same reference to static field

Upvotes: 1

Related Questions