Found 4336 Articles for Java 8

Role of super Keyword in Java Inheritance

Rishi Raj
Updated on 30-Jul-2019 22:30:24

454 Views

Parent class objects can be referred to using the super keyword in Java. It is usually used in the context of inheritance. A program that demonstrates the super keyword in Java is given as follows:Example Live Democlass A {    int a;    A(int x) {       a = x;    } } class B extends A {    int b;    B(int x, int y) {       super(x);       b = y;    }    void print() {       System.out.println("Value of a: " + a);       System.out.println("Value of b: " ... Read More

Deriving a Class in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

3K+ Views

A class can be derived from the base class in Java by using the extends keyword. This keyword is basically used to indicate that a new class is derived from the base class using inheritance. It can also be said that it is used to extend the functionality of the class.A program that demonstrates this is given as follows:Example Live Democlass A {    void funcA() {       System.out.println("This is class A");    } } class B extends A {    void funcB() {       System.out.println("This is class B");    } } public class Demo {   ... Read More

Create class Objects in Java using new operator

Arushi
Updated on 30-Jul-2019 22:30:24

478 Views

Class objects can be created in Java by using the new operator. A class is instantiated by the new operator by allocating memory for the object dynamically and then returning a reference for that memory. A variable is used to store the memory reference.A program that demonstrates this is given as follows:Example Live Democlass Student {    int rno;    String name;    public Student(int r, String n) {       rno = r;       name = n;    }    void display() {       System.out.println("Roll Number: " + rno);       System.out.println("Name: " + ... Read More

Add elements at the end of a Vector in Java

Arushi
Updated on 30-Jul-2019 22:30:24

311 Views

Elements can be added at the end of a Vector using the java.util.Vector.add() method. This method has a single parameter i.e. the element to be added. It returns true if the element is added to the Vector as required and false otherwise.A program that demonstrates this is given as follows:Example Live Demoimport java.util.Vector; public class Demo {    public static void main(String args[]) {       Vector vec = new Vector(5);       for (int i = 1; i <= 10; i++) {          vec.add(i);       }       System.out.println("The Vector elements ... Read More

How to use the asList() method of the Arrays class to create a Vector object

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

1K+ Views

A Vector can be created from an Array using the java.util.Arrays.asList() method.A program that demonstrates this is given as follows:Example Live Demoimport java.util.Arrays; import java.util.Vector; public class Demo {    public static void main(String args[]) {       Integer[] arr = { 3, 1, 9, 6, 4, 8, 7 };       Vector vec = new Vector(Arrays.asList(arr));       System.out.println("The Vector elements are: " + vec);    } }OutputThe Vector elements are: [3, 1, 9, 6, 4, 8, 7]Now let us understand the above program.The Integer Array arr[] is defined. Then a Vector is created using the ... Read More

Static Nested Classes in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

219 Views

A nested class in Java is of two types i.e. Static nested class and Inner class. A static nested class is a nested class that is declared as static. A nested nested class cannot access the data members and methods of the outer class.A program that demonstrates a static nested class is given as follows:Example Live Demopublic class Class1 {    static class Class2 {       public void func() {          System.out.println("This is a static nested class");       }    }    public static void main(String args[]) {       Class1.Class2 obj = ... Read More

Reference static field after declaration in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

176 Views

The static field variable is a class level variable and it belongs to the class not the class object.So the static field variable is common to all the class objects i.e. a single copy of the static field variable is shared among all the class objects.A program that demonstrates referencing the static field variable after declaration is given as follows:Example Live Demopublic class Demo {    static int a = 7;    static int b = a + 5;    public static void main(String[] args) {       System.out.println("a = " + a);       System.out.println("b = " + ... Read More

Demonstrate static variables, methods and blocks in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

7K+ Views

The static variable is a class level variable and it is common to all the class objects i.e. a single copy of the static variable is shared among all the class objects.A static method manipulates the static variables in a class. It belongs to the class instead of the class objects and can be invoked without using a class object.The static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    static int x = 10;   ... Read More

A static initialization block in Java

Arushi
Updated on 30-Jul-2019 22:30:24

8K+ Views

Instance variables are initialized using initialization blocks. However, the static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded. There can be multiple static initialization blocks in a class that is called in the order they appear in the program.A program that demonstrates a static initialization block in Java is given as follows:Example Live Demopublic class Demo {    static int[] numArray = new int[10];    static {       System.out.println("Running static initialization block.");       for (int i = 0; i < numArray.length; i++) {     ... Read More

A non-static initialization block in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

2K+ Views

Instance variables are initialized using initialization blocks. These blocks are executed when the class object is created and before the invocation of the class constructor. Also, it is not necessary to have initialization blocks in the class.A program that demonstrates a non-static initialization block in Java is given as follows:Example Live Demopublic class Demo {    static int[] numArray = new int[10];    {       System.out.println("Running non-static initialization block.");       for (int i = 0; i < numArray.length; i++) {          numArray[i] = (int) (100.0 * Math.random());       }    }   ... Read More

Advertisements