Some More Java Concepts

public class Main {
int x = 5;

public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}.
public class Main {
int x = 5;

public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Main myObj = new Main();
myObj.x = 40;
System.out.println(myObj.x).
  1. public — can be accessed from any class
  2. default - can be accessed in the same package.
  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.
  1. final — class cannot be inherited by another class
  2. abstract — class cannot create objects.
  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.
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);
}
}
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
}
}
Static methods can be called without creating objects
Public methods must be called by creating objects
  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/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store