Introduction to OOP — java file basic rules, access modifiers, classpath.
In this chapter, we are going to learn about basic rules to write a java file and learn access modifiers and classpath.
Basic Rules To Write A Java File
There are few rules (at least for now) you need to remember when you write a Java file:
- The file name should be the same as the class name with the extension .java.
- The file must contain one public class (This is not necessarily true but as a beginner, it will be easier for you to digest).
- The public class is created by preceding the class name public and class keywords. Look at the order of how those keywords occurred in the following code. The order is important. If you change the order you will get a compile error.
Considering our Toy class, first, create a file named Toy.java and then write the code in the above figure in that file. If you compile Toy.java it will give no error. But try to change the file name to toy.java and compile. You will get the following error:
toy.java:1: error: class Toy is public, should be declared in a file named Toy.java
public class Toy {
^
1 error
From the above error, it is clear that if there is a public class the file name (without extension) should be the same as the class name. A java file can contain only one public class. There is a concept of a nested class but for the time being you do not need to worry about it.
Introduction to Access Modifiers
Now there are some problems with our current Toy class definition:
public class Toy {
int ID;
int price;
int quantity;
}Let us see the first problem:
Toy car = new Toy();
car.ID = 10;
car.price = -10;
car.quanity = 10;Now we have created an object of type Toy which contains negative price. But, is it possible the price to be negative? the answer is no. So, with current class definition there is no way to prevent it.
Now another problem is if we change the member variable name ID to toyID like following:
public class Toy {
int toyID;
int price;
int quantity;
}Now the following code will not work anymore:
Toy car = new Toy();
car.ID = 10;
car.price = 30;
car.quanity = 10;You will get compile error because there is no property (member variable) of car which have name ID. To fix this you have to change the code to:
Toy car = new Toy();
car.toyID = 10;
car.price = 30;
car.quanity = 10;So changing inside the class is affecting the code we wrote outside of the class.
Get rakibul rushel’s stories in your inbox
Join Medium for free to get updates from this writer.
To remedy this problems, lets change the class definition by introducing private access modifier like following:
public class Toy {
private int ID;
private int price;
private int quantity;
}With the help of private access modifier we prevent direct access of the member variables.
Here is the implementation of the Main class from where the application starts. Here is the Main.java file:
public class Main {
public static void main(String[] args) {
Toy car = new Toy();
car.ID = 10;
car.price = 30;
car.quantity = 10;
}
}Note: Put your Toy.java in the same directory as the Main.java.
Now run the command javac Main.java. You will get the following error:
Main.java:4: error: ID has private access in Toy
car.ID = 10;
^
Main.java:5: error: price has private access in Toy
car.price = 30;
^
Main.java:6: error: quantity has private access in Toy
car.quantity = 10;
^
3 errors
The errors are basically saying that you cannot directly access member variables using the reference variable anymore as you did so far. For example, Main.java:6: error: quantity has private access in Toy — tells that you got compile error at line number 6 of the Main.java specifying the error.
We can remove those errors by changing the access modifier from private to public:
public class Toy {
public int ID;
public int price;
public int quantity;
}The public access modifier allows you to access the member variable from anywhere with the reference variable. If you now compile the Main.java file then the error will be gone.
When you compile Main.java there will be two class files generated by java compiler in the directory you put in those files. One is Toy.class and another is Main.class.
In the above figure you can see that Main.java and Toy.java are in folder
E:\java8-with-maven\OOP-2. When you compile Main.java java compiler knows you are using Toy.java so it compiles Toy.java also.
Actually, we can go one step further and delete Toy.java and compile Main.java again. You will see there is no compile error.
Look closely we do not have Toy.java in the folder. We have deleted it. Now, let us compile the Main.java again and see if we get any compile error.
So, there is no compile error, java compiler still knows about the Toy class. The reason is classpath.
Introduction to Classpath
The purpose of the classpath is to tell java compiler where to look for classes you defined, in particular it looks for files with .class extension. The default is the current directory where it looks for class files. So, when you use Toy class in Main.java it looks for Toy.class in the current directory and found the Toy.class file and got the definition of Toy class from there. We will see more practical usage of classpath in more advance discussion.
Now lets go back to our private example:
public class Toy {
private int ID;
private int price;
private int quantity;
}So the question now is if we cannot directly access the member variables through reference variable then how the hell we are going to set the value for ID, price and quantity? the answer to that is the method which we are going to learn in the next chapter.







