Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Constructor' - 4 code snippet(s) found

 Sample 1. Class bean with getter , setter and constructor

public class Employee {
public String name;
public int age;
public int salary;

Employee(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}
}

   Like      Feedback     bean   pojo   plain java objects   getters   setters   getters nd setters   classes   class   constructor   parameterized constructor  this keyword


 Sample 2. Singleton Class ( using private constructor , object initialization using static method, doubly checked , thread safe, synchronized , volatile reference )

class Singleton {
   private static volatile Singleton instance = null;
   private Singleton(){}
   public static Singleton getInstance() {
      if (instance == null) {
         synchronized(Singleton.class) {
            if (instance== null)
               instance = new Singleton();
            }
         }
      return instance;
   }
}

   Like      Feedback     singleton   singleton class   volatile   private constructor    object initialization using static method   doubly checked    thread safe   synchronized block using class level lock   synchronized   synchronized block   class level lock


 Sample 3. Constructor Overloading

public class Employee {
public String name;
public int age;
public int salary;

// No Argument Constructor
Employee(){
    this.name = "";
    this.age = 0;
    this.salary = 0;
}

// Single Argument Constructor
Employee(String name){
    this.name = name;
    this.age = 0;
    this.salary = 0;
}

// Argument Constructor
Employee(String name,int age){
    this.name = name;
    this.age = age;
    this.salary = 0;
}

// Argument Constructor
Employee(String name,int age,int salary){
    this.name = name;
    this.age = age;
    this.salary = salary;
}
}

   Like      Feedback     constructor  constructor overloading


 Sample 4. Initialize member elements ( maps ) using constructor

public class CoinChanger {
   private static Map<Currency, Integer> cashBox;
   private static Map<Currency, Integer> change;

   enum Currency {
      DOLLAR,QUARTER,DIME,NICKEL,PENNY;
   }

   CoinChanger() {
      cashBox = new TreeMap<Currency, Integer>();
      change = new TreeMap<Currency, Integer>();
      initializeCashBox();
   }

   private static void initializeCashBox(){
      //set the cash box
      cashBox.put(Currency.DOLLAR, 50);
      cashBox.put(Currency.QUARTER, 0);
      cashBox.put(Currency.DIME, 50);
      cashBox.put(Currency.NICKEL, 50);
      cashBox.put(Currency.PENNY, 50);
   }
}
}

   Like      Feedback     constructor   initializing maps   initializing treemap   enum



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus