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)
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)
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)
- 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)
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)
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);
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();
}}
// 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);
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)