- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Does static factory method internally use new keyword to create object in java?
Factory pattern is a design pattern (creational pattern) which is used to create multiple objects based on the data we provide. In it we create an object abstracting the process of creation.
Example
Below given is the example implementation of the factory pattern. Here, we have an interface with name Employee and 3 classes: Student, Lecturer, NonTeachingStaff, implementing it. We have created a factory class (EmployeeFactory) with a method named getEmployee(). This method accepts a String value and returns an object of one of the classes, based on the given String value.
import java.util.Scanner; interface Person{ void dsplay(); } class Student implements Person{ public void dsplay() { System.out.println("This is display method of the Student class"); } } class Lecturer implements Person{ public void dsplay() { System.out.println("This is display method of the Lecturer class"); } } class NonTeachingStaff implements Person{ public void dsplay() { System.out.println("This is display method of the NonTeachingStaff class"); } } class PersonFactory{ public Person getPerson(String empType) { if(empType == null){ return null; } if(empType.equalsIgnoreCase("student")){ return new Student(); } else if(empType.equalsIgnoreCase("lecturer")){ return new Lecturer(); } else if(empType.equalsIgnoreCase("non teaching staff")){ return new NonTeachingStaff(); } return null; } } public class FactoryPattern { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the type of the object you want: (student, lecturer, non teaching staff)"); String type = sc.next(); PersonFactory obj = new PersonFactory(); Person emp = obj.getPerson(type); emp.dsplay(); } }
Output
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is display method of the Lecturer class
Static factory method
Though it is said that there are five ways to create an object in Java namely −
- Using the new keyword.
- Using the Factory method.
- Using cloning.
- Using Class.forName().
- Using object deserialization.
The only way to create an object in Java is using the new keyword all other ways are abstractions to it. Intact all these methods internally use the new keyword.
Example
import java.util.Scanner; interface Employee{ void dsplay(); } class Student implements Employee{ public void dsplay() { System.out.println("This is display method of the Student class"); } } class Lecturer implements Employee{ public void dsplay() { System.out.println("This is display method of the Lecturer class"); } } class NonTeachingStaff implements Employee{ public void dsplay() { System.out.println("This is display method of the NonTeachingStaff class"); } } class EmployeeFactory{ public static Employee getEmployee(String empType) { if(empType == null){ return null; } if(empType.equalsIgnoreCase("student")){ return new Student(); } else if(empType.equalsIgnoreCase("lecturer")){ return new Lecturer(); } else if(empType.equalsIgnoreCase("non teaching staff")){ return new NonTeachingStaff(); } return null; } } public class FactoryPattern { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the type of the object you want: (student, lecturer, non teaching staff)"); String type = sc.next(); EmployeeFactory obj = new EmployeeFactory(); Employee emp = EmployeeFactory.getEmployee(type); emp.dsplay(); } }
Output
Enter the type of the object you want: (student, lecturer, non teaching staff) lecturer This is display method of the Lecturer class
- Related Articles
- Can we use "this" keyword in a static method in java?
- static Keyword in Java
- Java static keyword
- Factory method to create Immutable List in Java SE 9
- Factory method to create Immutable Map in Java SE 9
- Factory method to create Immutable Set in Java SE 9
- Is it possible to use this keyword in static context in java?
- Why can't we use the "super" keyword is in a static method in java?
- static Keyword in Java programming
- New keyword in Java
- static keyword in C++ vs Java
- Factory method to create Immutable List in android?
- Factory method to create Immutable Set in android?
- What is the 'new' keyword in JavaScript to create an object and how to access values?
- What is the use of ‘new’ keyword in C#?
