Content about Java ERP Solution for Reviews Best ERP Software for sale and Free Software with Java OpenSource for Free Download

Java synchronized block vs synchronized method

You can using synchronized  to lock multi Thread program with process thread by thread

Example code for using synchronized block and synchronized  method

synchronized  method

public synchronized void testMethod(String obj){
    // you method logic  
}

synchronized block
public void testMethod(String obj){
    // your method logic
    synchronized(this){
        // your method logic
        // your method logic
    }
    // your method logic
    // your method logic
    synchronized(Test.class){
        // your method logic
        // your method logic
    }
    // your method logic
}

Java enum valueof example code for valueof method in java enum

enum  is the common base class of all Java language enumeration types. including descriptions of the implicitly declared methods synthesized by the compiler
valueof method returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type

Method syntax

public static > T valueOf(Class enumType, String name)

Method input parameter

    enumType - the Class object of the enum type from which to return a constant
    name - the name of the constant to return

Method return

the enum constant of the specified enum type with the specified name

Example Code for java enum valueof

 enum Car {  
   City(800000), Civic(1200000),Jaz(850000);   
   int price;  
   Car(int p) {  
    price = p;  
   }  
   int showPrice() {  
    return price;  
   }  
 }  
 public class EnumTest {  
   public static void main(String args[]) {  
    System.out.println("My Car List:");  
    for(Car m : Car.values()) {  
     System.out.println(m + " Price " + m.showPrice());  
    }  
    Car city;  
    city = Car.valueOf("City");  
    System.out.println("Test Selected Car : " + city);                 
   }  
 }  


Runnint Result

My Car List:
City Price 800000
Civic Price 1200000
Jaz Price 850000
Test Selected Car : City