Found 267 Articles for Java8

Creating multiple Java objects by one type only

Ankitha Reddy
Updated on 30-Jul-2019 22:30:22

1K+ Views

You can create a List of object easily. Consider the following example, where I'll create an array of Employee objects and print their details in a for loop. import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; public class Tester implements Cloneable { private int data; public int getData() { return data; } public void setData(int data) { this.data = data; } public Tester(int data){ ... Read More

5 different ways to create objects in Java

Nikitha N
Updated on 06-Mar-2020 06:12:45

4K+ Views

Consider a class Tester which has implemented Cloneable interface. Now you can initialize an object using following five ways:1. Using new keyword.Tester tester1 = new Tester();2. Using Class.forName() methodTester tester2 = (Tester)Class.forName("Tester").newInstance();3. Using clone method.Tester tester3 = tester1.clone();4. Using Constructor.forName() methodTester tester4 = Tester.class.getConstructor().newInstance();5. Using DeserializationObjectInputStream objectInputStream = new ObjectInputStream(inputStream ); Tester tester5 = (MyObject) objectInputStream.readObject(); Using new keyword is the most preferred one.

3 ways to initialize an object in Java

Syed Javed
Updated on 06-Mar-2020 06:09:29

7K+ Views

Consider a class Tester which has implemented Cloneable interface. Now you can initialize an object using following three ways −1. Using new keyword.Tester tester1 = new Tester();2. Using Class.forName() methodTester tester4 = (Tester)Class.forName("Tester").newInstance();3. Using clone method.Tester tester2 = tester1.clone();

New keyword in Java

Nikitha N
Updated on 06-Mar-2020 06:08:28

550 Views

Yes, it is similar to a new keyword of C++. a new keyword is used to initialize/create an object. See the following example −Employee employee = new Employee();Here new keyword is used to create an object of class Employee.new Employee() invokes the constructor of the class Employee.new keyword can also be used without assigning the object to a reference variable. See the example −String name = new Employee().getName();Here we are creating an object using new keyword and then invoked a method getName() on the object and passed the result to a variable.

CamelCase in Java naming conventions

Ankitha Reddy
Updated on 30-Jul-2019 22:30:22

734 Views

Java follows camel casing for objects, class, variables etc. If a name is having multiple words, the first letter is small then consecutive words are joint with the first letter as a capital case. Consider the following example − Taxation Department Class - TaxationDepartment Object - taxationDepartment Method - getTaxationDepartmentDetails Variable - taxationDepartment

Advantages of naming conventions in Java

Syed Javed
Updated on 06-Mar-2020 05:38:32

340 Views

Following the the best practices while declaring a variable.  These best practices maintains code readability, understandability as project code size increases.Variables names should be short or long enough as per the scope. For example, loop counter variable, i is fine whereas employee as loop variable.Specific words should not be used like equals, compare, data.Use meaningful names which can explain the purpose of the variable. For example cnt Vs counter.Don't use _ to declare a variable name, Use camel casing. For example, employeeName is better than employee_name.Each organization has its own syntax specific standards. Follow those rules to maintain consistency and ... Read More

Java Naming Conventions

Ankitha Reddy
Updated on 03-Jul-2024 22:31:47

2K+ Views

All Java components require names. Names used for classes, variables and methods are called identifiers. The naming conventions for different Java components are as follows: Package Naming Convention Class Naming Convention Interfaces Naming Convention Method Naming Convention Constants Naming Convention> Variables Naming Convention Quick Summary Package Naming Convention Naming conventions for Java packages typically involve using lowercase letters. It's common to use the reverse domain name as a prefix to ensure uniqueness. Example com.tutorialspoint.project Class Naming Convention When naming classes in Java, it's essential to start the class name with an uppercase letter. ... Read More

What is the purpose of private constructor in Java?

Syed Javed
Updated on 18-Jun-2020 07:56:40

644 Views

The private constructor is useful in case we want to restrict the object creation. For example, Singleton pattern can be implemented using a private constructor.ExampleLive Demopublic class Tester {    private static Tester instance;    private Tester(){}      public static Tester getInstance(){       if(instance == null){          instance = new Tester();       }       return instance;    }      public static void main(String[] args) {       Tester tester = Tester.getInstance();       Tester tester1 = Tester.getInstance();       System.out.println(tester.equals(tester1));    }   }OutputIt will print the output astrue

How is down-casting possible in Java?

Syed Javed
Updated on 12-Mar-2020 10:34:57

113 Views

Yes, a variable can be downcast to its lower range substitute by casting. It may lead to data loss although. See the example below −ExampleLive Demopublic class Tester {      public static void main(String[] args) {       int a = 300;         byte b = (byte)a;         System.out.println(b);    }   }OutputIt will print output as −44

What is runtime polymorphism or dynamic method overloading?

Ankitha Reddy
Updated on 05-Mar-2020 12:29:43

1K+ Views

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Method overriding by a subclass is termed as runtime polymorphism. JVM determines the method to be executed at runtime instead of compile time. exampleLive Democlass SuperClass {    SuperClass get(){       System.out.println("SuperClass");       return this;    } } public class Tester extends SuperClass {    Tester get(){       System.out.println("SubClass");       return this;    }    public static void main(String[] args) {       SuperClass tester = new Tester();         tester.get();    }   }OutputSubClass

Advertisements