0

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.

Markus
  • 3,225
  • 6
  • 35
  • 47
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
  • You call `println` on the object `System.out`. Your code is perfectly OOP. – StackedCrooked May 14 '15 at 18:59
  • 1
    You could create an application without having to create any "objects". For example, you could create a calculator app right there in that main method using a while loop, and some other "static" methods. Java is a good language for OO programming, but its not as hardcore as SmallTalk where everything is an Object. – Mecon May 14 '15 at 20:04

7 Answers7

2

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!)

Community
  • 1
  • 1
wizebin
  • 730
  • 5
  • 15
  • 1
    Don't forget about `System.out`, which is also created by Java. As well as the `String` object `"Java drives the web"` (which is a so-called objectionable propaganda literal, so you object it isn't an objective `Object` but it's objectively an `Object`) – sehe May 14 '15 at 19:02
  • 1
    There is no such thing as a "default object". No instance of the class `day1` will be created. No object is necessary to call the `main` method on, since the `main` method is `static`. – Jesper May 14 '15 at 19:34
1

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

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
1

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

jbarrueta
  • 4,907
  • 2
  • 20
  • 21
1

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.

Display Name
  • 8,022
  • 3
  • 31
  • 66
1

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).

paxmemento
  • 281
  • 4
  • 10
1

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.

MadConan
  • 3,749
  • 1
  • 16
  • 27
  • so a class can also be called an object? an array has data only. I thought objects are made up of code and data both. how an array is an object? – KawaiKx May 15 '15 at 01:11
1

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.

Community
  • 1
  • 1
Bacteria
  • 8,406
  • 10
  • 50
  • 67
  • is println() call is made on object 'out' which is a member of another object 'System' – KawaiKx May 15 '15 at 01:38
  • 1
    No.System is not an object.It is a class.out is a static member in the System class , being an instance of PrintStream. As System class constructor is private hence to access out we do : class_name.out_static_member(System.out) summary: 1>System is a final class from java.lang package 2>out is the reference of PrintStream class and a static member of System class.3>println : is a method of PrintStream class – Bacteria May 15 '15 at 10:01