Without understanding Java objects, you will never be able to master the language. Java is an object-oriented programming language, and objects are the result of that style of programming. C++, another object-oriented language has its own objects, and they work similarly to those of Java. However, today we're gonna find out what these objects are and hopefully figure out how Java truly works.
When you look at the world around you, what do you see? You see objects. In programming, objects are nothing more than representations of things. It's actually pretty simple. The world around you is a representation of stuff. You look at something, and you can probably tell me its characteristics. It's an object. It may be more specific than that, it might be a car object or a keyboard object or a person object, but it is still an object. Java has a slightly different definition but it is almost identical to a real-life object.
All Java classes are the blueprints of objects. Period. Every program you've ever written with a main method inside of your class can be an object. Not all classes are used as objects, but they CAN all be objects regardless.
For example, Scanner is a Java object. It is also a class, but from now on we're going to call it an object. The Scanner object's characteristics are that it accepts user input in various different ways.
Let's try using one of your own classes as an object in other class. I'm going to show examples of two different classes.
There are my two classes. Now, I am going to use my Methods class as an object inside of HelloWorld. In order to do this, you have to make sure that both Java files are in the same folder. In Eclipse, you also have to make sure they are in the same package. If you've been creating all of your programs in the default package, then this will not be a problem. For those using other editors, just make sure both of your Java classes are in the same folder on your computer.
I created a new Methods object inside of my HelloWorld class. It was created the same way that I would create a Scanner, except that I did not have to put System.in inside of the parenthesis. I called this new object myObject, but I could have called it by another variable name.
Now that I have my new object inside of HelloWorld, I'm going to use it to access one of its methods. Notice how the Methods class has a method called method2().
Now run the program, you should see that it displays 3.14159 under Hello World! This is because that is what that method is supposed to do. You can look back at the Methods class to verify this.
Remember that with a Scanner, you did the same thing. You called the Scanner a name, and then used it in the same way to access its readLine() or readInt() methods.
Going back to our example, you will see that in Eclipse, you will receive a warning when doing this. Warnings do not prevent your programs from running, however they indicate bad style usually. We're going to ignore these warnings for now because they go beyond what we're covering here.
In the next tutorial, you'll learn how to create your own Java objects that aren't their own programs. This means that these objects will not have main methods. I would not proceed to that tutorial until you fully understand everything on this page, as it's super important you understand what an object is and how to use them before you do.
If you have any questions, comments, or concerns about this tutorial, feel free to contact us.