Java Articles

Page 338 of 450

5 different ways to create objects in Java

Nikitha N
Nikitha N
Updated on 06-Mar-2020 5K+ 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.

Read More

5 different ways to create objects in Java

Nikitha N
Nikitha N
Updated on 06-Mar-2020 5K+ 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.

Read More

New keyword in Java

Nikitha N
Nikitha N
Updated on 06-Mar-2020 787 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.

Read More

New keyword in Java

Nikitha N
Nikitha N
Updated on 06-Mar-2020 787 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.

Read More

Advantages of naming conventions in Java

Syed Javed
Syed Javed
Updated on 06-Mar-2020 646 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 ...

Read More

Advantages of naming conventions in Java

Syed Javed
Syed Javed
Updated on 06-Mar-2020 646 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 ...

Read More

What is runtime polymorphism or dynamic method overloading?

Ankitha Reddy
Ankitha Reddy
Updated on 05-Mar-2020 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. example class 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();     } } Output SubClass

Read More

What is runtime polymorphism or dynamic method overloading?

Ankitha Reddy
Ankitha Reddy
Updated on 05-Mar-2020 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. example class 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();     } } Output SubClass

Read More

How do you write a method in Java that can print object in array?

Every ; Thing
Every ; Thing
Updated on 05-Mar-2020 216 Views

Exampleclass Shape{ private String shapeName; private int numSides; Shape(String shapeName, int numSides){ this.shapeName = shapeName; this.numSides = numSides; } public String toString(){ return shapeName + " has " + numSides + " sides."; } } class ObjectList{ private Object[] list = new Object[10]; private int numElement = 0; public void add(Object next){ list[numElement] = next; numElement++; } @Override public String toString(){ String str=""; int i=0; while(list[i] != null){ str += list[i]+""; i++; } return str; } } public class Driver{ public static void main(String[] args){ ObjectList list = new ObjectList(); Shape square = new Shape("square", 4); Shape hex = new Shape("hexagon", 6); list.add(hex); list.add(square); System.out.println(list); } }

Read More

Importance of Collectors.flatMapping() method in Java 9?

raja
raja
Updated on 05-Mar-2020 600 Views

In Java 9, a new method added to the Collectors class: flatMapping(). It is similar to the Collectors.mapping() method in which the flatMapping() method allows us to handle nested collections. The Collectors.flatMapping() method takes a function to be applied to input elements and a collector to accumulate the elements passed through the function. Unlike the Collectors.mapping() method, the Collectors.flatMapping() method deals with a stream of elements that allows us to get rid of unnecessary intermediary collections.Syntaxpublic static Collector flatMapping(Function

Read More
Showing 3371–3380 of 4,498 articles
« Prev 1 336 337 338 339 340 450 Next »
Advertisements