Lots of concepts in this example. It might be wise to look at more simple ones. But I'll try to explain.
First question:
Let's look at this first:
class DataOnly
{
final int LIMIT = 100; //constant and package mode or access
int iMem; //package mode or access
double dMem; //package mode or access
}
This is a class. Think of it as a blueprint that can be used to create objects. (More accurately, blueprints should be called types). It's important to understand that a class itself is not the object. We can create several objects with the same type.
DataOnly data1 = new DataOnly();
You are right, this line creates an object. DataOnly
is a type, and data1
is a reference (more on that later). DataOnly data1
means that data1
will be a reference to an object that is created to match the type DataOnly
. new DataOnly
means that we are creating an object using that "blueprint" we defined. Where will the object be? It will be stored somewhere in the memory, and our reference data1
will hold that location in the memory (memory address) for us. Whenever we want to do something with our object, we use this reference to reach it in the memory. We could create a data2 object, which would be placed somewhere else in the memory, and change its members. They would not affect data1 in any way, because the class is the "blueprint", the object is the thing we created using that blueprint.
The () means that we are calling a parameterless constructor.
For your second question: if you understand my previous explanation, then this one should be no problem. We want to do something with object - print its members. Ok, we have our reference. The .
(dot) is used to access parts of the object. data1.LIMIT
will eventually mean that we take the object referenced by data1 and look at the LIMIT member of it.
Third question: Objects can not only hold information but can also do things. For example, you probably used a CD player before. If you look at it, you can see how many tracks the current CD has. But not only that, you can push the play button and it will play the song for you. Let's see how that would look in Java.
class CDPLayer
{
int numberOfTracks = 12;
void play()
{
System.out.println("I'm playing! Yay!");
}
}
This is a simple CD player, and there is no way to change the CD in it :)
The things an object has are its members, the things it can do are its methods. Our very simple CD Player has a number of tracks and can do playing. Methods and members are equally parts of the object, so we can use both with the dot .
to reach them. (remember, an object is not the same as the class)
At this point you will probably understand the following code:
CDPlayer player = new CDPlayer();
System.out.println("The number of tracks is " + player.numberOfTracks);
player.play();
We create a CDPlayer and then print the number of tracks in it. We then use its play
method.
In your example, the MethodOnly class declares two methods with the same name. This is probably confusing, but the compiler does not determine which method to use by name, but by signature. The signature of the method includes its return type, its name, and all of its parameters. What happens when you call printFunc
? The compiler looks at the type of the arguments you passed, and will do it's best to find the method that matches that.
The first time it is called with an argument 5
, which is an integer, or int for short. The compiler selects the printFunc with the int parameter
Second time it is called with a "string literal" so the compiler selects the printFunc with the String parameter
Third time it is called with a variable. Which variable is it? It's data1's LIMIT member. We go and check the blueprint to see its type: final int
. final
is tricky and might mean different things in different contexts. This time it means that the variable can only be set once during the program and cannot be changed later. So now what? Which printFunc does the compiler choose? There is no final int
version. As I said before, the compiler tries to find the best one, and in this case, a simple int parameter is good enough. If it can't find a good enough match, it will stop and print an error message.