Tutorialspoint

Java to use the final modifier

class TestExample {
   final int value = 10;
   public static final int BOXWIDTH = 6;
   static final String TITLE = "Manager";
   
   public final void changeName() {
      System.out.println("This is a final method");
   }
}
final class Demo{ }
public class FinalExample extends TestExample {
   public static void main(String args[]){
      FinalExample obj = new FinalExample();
      System.out.println(obj.value);
      System.out.println(obj.BOXWIDTH);
      System.out.println(obj.TITLE);
      obj.changeName();
   }
}

Java Synchronized Modifier

public class TestThread {
   public static Object Lock1 = new Object();
   public static Object Lock2 = new Object();
   
   public static void main(String args[]) {
      ThreadDemo1 T1 = new ThreadDemo1();
      ThreadDemo2 T2 = new ThreadDemo2();
      T1.start();
      T2.start();
   }
   private static class ThreadDemo1 extends Thread {
      public void run() {
         synchronized (Lock1) {
            System.out.println("Thread 1: Holding lock 1...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 1: Waiting for lock 2...");
            
            synchronized (Lock2) {
               System.out.println("Thread 1: Holding lock 1 & 2...");
            }
         }
      }
   }
   private static class ThreadDemo2 extends Thread {
      public void run() {
         synchronized (Lock2) {
            System.out.println("Thread 2: Holding lock 2...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 2: Waiting for lock 1...");
            
            synchronized (Lock1) {
               System.out.println("Thread 2: Holding lock 1 & 2...");
            }
         }
      }
   }
}

Java Synchronized Keyword

public class TestThread {
   public static Object Lock1 = new Object();
   public static Object Lock2 = new Object();
   
   public static void main(String args[]) {
      ThreadDemo1 T1 = new ThreadDemo1();
      ThreadDemo2 T2 = new ThreadDemo2();
      T1.start();
      T2.start();
   }
   private static class ThreadDemo1 extends Thread {
      public void run() {
         synchronized (Lock1) {
            System.out.println("Thread 1: Holding lock 1...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 1: Waiting for lock 2...");
            
            synchronized (Lock2) {
               System.out.println("Thread 1: Holding lock 1 & 2...");
            }
         }
      }
   }
   private static class ThreadDemo2 extends Thread {
      public void run() {
         synchronized (Lock2) {
            System.out.println("Thread 2: Holding lock 2...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 2: Waiting for lock 1...");
            
            synchronized (Lock1) {
               System.out.println("Thread 2: Holding lock 1 & 2...");
            }
         }
      }
   }
}

Java Final Modifier

class TestExample {
   final int value = 10;
   public static final int BOXWIDTH = 6;
   static final String TITLE = "Manager";
   
   public final void changeName() {
      System.out.println("This is a final method");
   }
}
final class Demo{ }
public class FinalExample extends TestExample {
   public static void main(String args[]){
      FinalExample obj = new FinalExample();
      System.out.println(obj.value);
      System.out.println(obj.BOXWIDTH);
      System.out.println(obj.TITLE);
      obj.changeName();
   }
}

Java Abstract Keyword

abstract class Employee {
   private String name;
   private String address;
   private int number;
   
   public Employee(String name, String address, int number) {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public double computePay() {
      System.out.println("Inside Employee computePay");
      return 0.0;
   }
   public void mailCheck() {
      System.out.println("Mailing a check to " + this.name + " " + this.address);
   }
   public String toString() {
      return name + " " + address + " " + number;
   }
}
public class AbstractDemo extends Employee{
      public AbstractDemo(String name, String address, int number) {
      super(name, address, number);
   }
   public static void main(String [] args) {
      Employee e = new AbstractDemo("George W.", "Houston, TX", 43);
      System.out.println("\n Call mailCheck using Employee reference--");
      e.mailCheck();
   }
}

Java final parameter

public class Test{
   public void sample(final int data){
      System.out.println(data);
   }
   public static void main(String args[]) throws Exception{
      Test t = new Test();
      t.sample(500);
   }
}

Java class variables, instance variables and local variables

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

Java difference between class variables and instance variables

public class VariableExample{
   int myVariable;
   static int data = 30;
   
   public static void main(String args[]){
      VariableExample obj = new VariableExample();
      
      System.out.println("Value of instance variable: "+obj.myVariable);
      System.out.println("Value of static variable: "+VariableExample.data);
   }
}

Java difference between integer and floating point literals

public class StringExample {
   public static void main(String args[]){
      int num1 = 100;
      float num2 = 30.0f;
      System.out.println("Value of integer:"+num1);
      System.out.println("Value of integer:"+num2);
   }
}

Java difference between character literals and string literals

public class LiteralsExample {
   public static void main(String args[]){
      char ch = 'H';
      String str = "Hello";
      
      System.out.println("Value of character: "+ch);
      System.out.println("Value of string: "+str);
   }
}

1 2 3 4 5 6 7 ... 126 Next
Advertisements
Loading...