
Some More Java Concepts
In the previous two articles, I discussed object oriented programming in Java and some concepts that are good to know. In the second article, the concepts I discussed are how to make a class and constructors and print out words. This article will include some concepts not discussed in the previous two.
Let’s look at this code
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}.
This will print out 5. In this code, the integer x = 5 is an attribute of the class. We can access the attribute by
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
or we can modify it by replacing the inside of the public static void with
Main myObj = new Main();
myObj.x = 40;
System.out.println(myObj.x).
This will change the value from x = 5 to x = 40.
Let’s discuss modifiers. The word public from public class Main
is an access modifier. That means I can the Main class from any class. There are two access modifiers for a class. They are
- public — can be accessed from any class
- default - can be accessed in the same package.
For attributes, methods, and constructors, they are
- public — same as above
- private — can only be accessed in the same class
- default — same
- protected — can be accessed in the same package and subclass.
There are also non-access modifiers. For classes, they are
- final — class cannot be inherited by another class
- abstract — class cannot create objects.
For attributes and methods, they are
- final — cannot be overridden or changed
- static — they belong to a class
- abstract — can be used in abstract class only
- transient — are skipped when serializing the object that contain them
- synchronized — methods are accessed by one thread at a time
- volatile — attribute is not thread-locally, but read from main memory.
Let’s try to look at final. We cannot override or change a value we set. Let’s see it in code.
public class Main {
final int x = 10;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 50;
System.out.println(myObj.x);
}
}
First we set x = 10; then we try to set x = 50. If we were to run it, we receive an error “error: cannot assign a value to final variable x”.
Now let’s look at static and public. A public modifier can be called without using an object; static modifiers do.
public class Main {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
// Main method
public static void main(String[ ] args) {
myStaticMethod(); // Call the static method
Main myObj = new Main(); // Create an object of Main
myObj.myPublicMethod(); // Call the public method
}
}
The last two lines will print
Static methods can be called without creating objects
Public methods must be called by creating objects
respectively.
We have seen attributes and some modifiers in this article. I hope to discuss some more at a later article.
References