- Accessor Methods

A method called an accessor retrieves confidential information that is kept inside an object. An accessor offers a way for other program elements to access an object's state.

class Student {

  //Instance variable name
  private String name;

  /** getName() example
   *  @return name */
  public String getName()
  {
     return name;
  }

  public static void main(String[] args)
  {
     // To call a get method, use objectName.getVarName()
     Student s = new Student();
     System.out.println("Name: MasterCoder " + s.getName() );
  }
}

Student.main(null)
Name: MasterCoder null
- Mutator Methods

A technique used to regulate changes to a variable is known as a mutator method. They're also frequently referred to as setter methods. A getter, often referred to as an accessor, frequently follows a setter and returns the value of the private member variable.

import java.math.BigDecimal;

public class Product {

    private BigDecimal price;
    private String name;

    public BigDecimal getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }
	
    // mutator or setter methods
     public void setPrice(BigDecimal price) {
        if(price.compareTo(BigDecimal.ZERO)<0 )throw new IllegalArgumentException("Empty product name");
        this.price = price;
    }

    public void setName(String name) {
        if(name.isEmpty()) throw new IllegalArgumentException("Empty product name");
        this.name = name;
    }
}
- Static, Class Variables

All instances of a class share static variables, which are variables. This implies that no matter how many instances of the same class of objects you create, changing a static variable or method will update it for all of them. This is helpful if you want to increment a counter for each instance of a class, for example.

class VariableDemo
{
   static int count=0;
   public void increment()
   {
       count++;
   }
   public static void main(String args[])
   {
       VariableDemo obj1=new VariableDemo();
       VariableDemo obj2=new VariableDemo();
       obj1.increment();
       obj2.increment();
       System.out.println("Obj1: count is="+obj1.count);
       System.out.println("Obj2: count is="+obj2.count);
   }
}
VariableDemo.main(null)
Obj1: count is=2
Obj2: count is=2
public class VariableExample{
   int myVariable;
   static int data = 30;
   
   public static void main(String args[]){
      int a = 100;
      VariableExample obj = new VariableExample();
      
      System.out.println("Value of instance variable myVariable: "+obj.myVariable);
      System.out.println("Value of static variable data: "+VariableExample.data);
      System.out.println("Value of local variable a: "+a);
   }
}

VariableExample.main(null)
Value of instance variable myVariable: 0
Value of static variable data: 30
Value of local variable a: 100
- Public, Private, Protected

public: available to everyone. protected: open to subclasses in any package as well as classes belonging to the same package. default, indicating that no modifiers are specified: accessible by classes belonging to the same package. private: only available to members of the same class.

public class Addition {

   public int addTwoNumbers(int a, int b){
	return a+b;
   }
}

class Test{
   public static void main(String args[]){
      Addition obj = new Addition();
      System.out.println(obj.addTwoNumbers(100, 1));
   }
}

Test.main(null)
101
class Data {
    private String name;

    // getter method
    public String getName() {
        return this.name;
    }
    // setter method
    public void setName(String name) {
        this.name= name;
    }
}
public class Main {
    public static void main(String[] main){
        Data d = new Data();

        // access the private variable using the getter and setter
        d.setName("Mortensen");
        System.out.println(d.getName());
    }
}

Main.main(null)
Mortensen
public class Addition {

   protected int addTwoNumbers(int a, int b){
	return a+b;
   }
}

class Test extends Addition{
   public static void main(String args[]){
	Test obj = new Test();
	System.out.println(obj.addTwoNumbers(11, 22));
   }
}
- Static, Class Methods

A static method is a method that is declared as a member of an object but can only be accessed from the constructor of an API object rather than from an instance of the object generated by the constructor. Class methods, on the other hand, are methods that are invoked on the class as a whole rather than on a single object instance. As a result, it falls under the class level, and the class method is shared by all class instances. A class method is linked to the class itself, not to any class objects. Only class variables are accessible to it.

public class StaticExample {

    public static int x;

    public static void main(String[] args) {
        x += 7;
        System.out.println("Add 7. Result: " + x);
    }

}

StaticExample se = new StaticExample();

se.main(null);
Add 7. Result: 7
- "this Keyword"

In a method or constructor, the "this keyword" refers to the current object. As a class attribute is shadowed by a method or constructor argument, the this keyword is most frequently used to clarify the differences between class attributes and parameters with the same name.

class A{  

    A(){  
this(5);  

System.out.println("hello world");  }  
    A(int x){  
    System.out.println(x);  }  

}  
    class TestThis6{  
    public static void main(String args[]){  
A a=new A();  
}}
- "main method"

As the starting point for running a Java program, the main method in Java is typically the first method you encounter when learning how to program in Java. The main method can be located in any class that is a part of a program and can contain code to run or call other methods.

// Extends static example and modifies x value
public class ExtendsExample extends StaticExample {

    public static void main(String[] args) {
        x += 10232;
        System.out.println("Add 10232. Result: " + x);
    }

}

ExtendsExample ee = new ExtendsExample();

ee.main(null);
Add 10232. Result: 10239
- Inherticance, Extends

It can inherit properties and methods from the class it is extending by using an extends method. When you want to alter or add to an existing class without having to completely rewrite the code for the class you are extending, this is helpful.

public class Cat extends Animal{

	private String color;

	public Cat(boolean veg, String food, int legs) {
		super(veg, food, legs);
		this.color="White";
	}

	public Cat(boolean veg, String food, int legs, String color){
		super(veg, food, legs);
		this.color=color;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

}

public class AnimalInheritanceTest {
	public static void main(String[] args) {
		Cat cat = new Cat(false, "milk", 4, "black");

		System.out.println("Cat is Vegetarian?" + cat.isVegetarian());
		System.out.println("Cat color is " + cat.getColor());
	}

}

AnimalInheritanceTest.main(null)
Cat is Vegetarian?false
Cat color is black