Some More Java Concepts

Darrick Pang
3 min readApr 16, 2021

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

  1. public — can be accessed from any class
  2. default - can be accessed in the same package.

For attributes, methods, and constructors, they are

  1. public — same as above
  2. private — can only be accessed in the same class
  3. default — same
  4. protected — can be accessed in the same package and subclass.

There are also non-access modifiers. For classes, they are

  1. final — class cannot be inherited by another class
  2. abstract — class cannot create objects.

For attributes and methods, they are

  1. final — cannot be overridden or changed
  2. static — they belong to a class
  3. abstract — can be used in abstract class only
  4. transient — are skipped when serializing the object that contain them
  5. synchronized — methods are accessed by one thread at a time
  6. 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

  1. https://darrickpang90.medium.com/java-and-object-oriented-programming-8692fdbff06
  2. https://darrickpang90.medium.com/java-concepts-f3f636f8735b
  3. https://www.w3schools.com/java/

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response