Found 9775 Articles for Object Oriented Programming

How do I declare and initialize an array in Java?

karthikeya Boyini
Updated on 19-Feb-2020 10:06:11
You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;You can access the array element using the index values −System.out.println("The first element of the array is: " + myArray [0]); System.out.println("The first element of the array is: " + myArray [1]); Alternatively, you can create and initialize an array using the flower braces ({ }): Int [] myArray = {10, 20, 30, 40, 50}

How to call one constructor from another in Java?

Giri Raju
Updated on 30-Jul-2019 22:30:20
You can call one constructor from another using this(). Example This is a default constructor This is parameterized constructor Live Demo public class Sample { int num; public Sample() { System.out.println("This is default constructor"); num = 30; } public Sample(int value) { this(); System.out.println("This is parameterized constructor"); num = value; } public static void main(String args[]){ Sample s = new Sample(30); } } Output This is default constructor This is parameterized constructor

How to declare an Array Variables in Java?

Sharon Christine
Updated on 19-Feb-2020 10:04:43
You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;You can access the array element using the index values −System.out.println("The first element of the array is: " + myArray [0]); System.out.println("The first element of the array is: " + myArray [1]); Alternatively, you can create and initialize an array using the flower braces ({ }): Int [] myArray = {10, 20, 30, 40, 50}

What is a composition in Java?

Lakshmi Srinivas
Updated on 30-Jul-2019 22:30:20
The composition is also a type of aggregation where the relationship is restrictive i.e. If two objects are in composition, the composed object will not exist without the other.

How to declare a constructor in Java?

Sreemaha
Updated on 19-Feb-2020 07:16:09
While declaring the constructors you should keep the following points in mind.A constructor does not have a return type.The name of the constructor is same as the name of the class. A class can have more than one constructor.Examplepublic class Sample {    int num;    public Sample() {       num = 30;    }    public Sample(int value) {       num = value;    } }

What is aggregation in Java?

Monica Mona
Updated on 30-Jul-2019 22:30:20
When an object A contains a reference to another object B or we can say Object A has a HAS-A relationship with Object B, then it is termed as Aggregation. Aggregation helps in reusing the code. Object B can have utility methods and which can be utilized by multiple objects. Whichever class has object B then it can utilize its methods. Example public class Vehicle{} public class Speed{} public class Van extends Vehicle { private Speed sp; } This shows that class Van HAS-A Speed. By having a separate class for Speed, we do ... Read More

What is a singleton class in Java?

karthikeya Boyini
Updated on 24-Feb-2020 09:44:19
A singleton class in Java is the one which can have only one object.The easiest implementation consists of a private constructor and a field to hold its result, and a static accessor method with a name like getInstance(). The private field can be assigned from within a static initializer block or, more simply, using an initializer. The getInstance( ) method (which must be public) then simply returns this instance −Examplepublic class Singleton {    private static Singleton singleton = new Singleton(); private Singleton() { }        public static Singleton getInstance() {       return singleton;    } ... Read More

When should I use the keyword ‘this’ in a Java class?

Sharon Christine
Updated on 30-Jul-2019 22:30:20
The this is a keyword in Java which is used as a reference to the object of the current class, within an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables, and methods. Example Live Demo public class This_Example { // Instance variable num int num = 10; This_Example() { System.out.println("This is an example program on keyword this"); } This_Example(int num) { ... Read More

How many public classes of the same name it can have in Java?

Swarali Sree
Updated on 30-Jul-2019 22:30:20
A Java file contains only one public class with a particular name. If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error. Example public class Example { } public class Example{ public void sample(){ System.out.println("sample method of the Example class"); } public void demo(){ System.out.println("demo method of the Example class"); } ... Read More

What does Integer.parseInt() method do in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:20
This is a static method of the class named Integer it accepts an integer parameter and Parses it as a signed decimal integer. Example Live Demo public class IntegerDemo { public static void main(String[] args) { // parses the string argument int a = Integer.parseInt("12"); int b = Integer.parseInt("26"); int c = Integer.parseInt("54"); int m = a * b * c; System.out.print("Value after multiplying = " + m); } } Output Value after multiplying = 16848
Advertisements