Create a Private Constructor in Java

Swarali Sree
Updated on 25-Feb-2020 10:17:06

346 Views

We can use a private contractor in a Java while creating a singleton class. The Singleton's purpose is to control object creation, limiting the number of objects to only one. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources, such as database connections or sockets.ExampleThe 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 ... Read More

What Does the Method ensureCapacity(int minCapacity) Do in Java

Govinda Sai
Updated on 25-Feb-2020 10:10:30

153 Views

The ensureCapacity(int minCapacity) method of the class java.util.ArrayList increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String args[]) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(10);       arrlist.add(50);       arrlist.add(30);       arrlist.ensureCapacity(15);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }OutputNumber = 10 Number = 50 Number = 30

What Does the Method firstElement Do in Java

Paul Richard
Updated on 25-Feb-2020 10:09:47

155 Views

The firstElement() method is used to return the first component (the item at index 0) of this vector.Exampleimport java.util.Vector; public class VectorDemo {    public static void main(String[] args) {       Vector vec = new Vector(4);       vec.add(4);       vec.add(3);       vec.add(2);       vec.add(1);       System.out.println("First element is :"+vec.firstElement());    } }OutputFirst element is :4

What Does the Method lastElement Do in Java

Arushi
Updated on 25-Feb-2020 10:08:06

147 Views

The lastElement() method is used to return the last component of the vector.Exampleimport java.util.Vector; public class VectorDemo {    public static void main(String[] args) {       Vector vec = new Vector(4);       vec.add(4);       vec.add(3);       vec.add(2);       vec.add(1);       System.out.println("Last element: "+vec.lastElement());    } }OutputLast element: 1

What Does the Method removeAllElements Do in Java

Rishi Raj
Updated on 25-Feb-2020 10:07:20

146 Views

The removeAllElements() method is used to remove all components from this vector and sets its size to zero. This method is identical in functionality to the clear method.Exampleimport java.util.Vector; public class VectorDemo {    public static void main(String[] args) {       Vector vec = new Vector(4);       vec.add(4);       vec.add(3);       vec.add(2);       vec.add(1);       System.out.println("Added numbers are :- ");       for (Integer number : vec) {          System.out.println("Number = " + number);       }       System.out.println("Size of ... Read More

What Does the Method int Capacity Do in Java

Vikyath Ram
Updated on 25-Feb-2020 10:06:15

343 Views

The capacity() method is used to return the current capacity of a vector. Capacity is the length of the array kept in the vector.Exampleimport java.util.Vector; public class VectorDemo {    public static void main(String[] args) {       Vector vec = new Vector();       vec.add(14);       vec.add(13);       vec.add(12);       vec.add(11);       vec.add(4);       vec.add(3);       vec.add(2);       vec.add(1);       System.out.println("Capacity of the vector is :"+vec.capacity());    } }OutputCapacity of the vector is :10

What Does the Method elements() Do in Java

Paul Richard
Updated on 25-Feb-2020 10:04:48

174 Views

The elements() method is used to return an enumeration of the components of this vector. The returned Enumeration object will generate all items in this vector at the similar index location.Exampleimport java.util.Vector; public class VectorDemo {    public static void main(String[] args) {             Vector vec = new Vector(4);       vec.add(4);       vec.add(3);       vec.add(2);       vec.add(1);       Enumeration e=vec.elements();       System.out.println("Numbers in the enumeration are :- ");       while (e.hasMoreElements()) {          System.out.println("Number = " + e.nextElement());       }    } }OutputNumbers in the enumeration are :- Number = 4 Number = 3 Number = 2 Number = 1

What Does the Method push(Object item) Do in Java

Arushi
Updated on 25-Feb-2020 10:04:00

560 Views

The push(Object item) method is used to Pushes an item onto the top of this stack.Exampleimport java.util.*; public class StackDemo {    public static void main(String args[]) {       Stack st = new Stack();       st.push("Java");       st.push("Source");       st.push("code");       System.out.println("Elements in the stack: "+st);    } }OutputElements in the stack: [Java, Source, code]

What Does the Method Peek Do in Java

Rishi Raj
Updated on 25-Feb-2020 10:01:14

321 Views

The peek() method is used to look at the object at the top of this stack without removing it from the stack.Exampleimport java.util.*; public class StackDemo {    public static void main(String args[]) {             Stack st = new Stack();       st.push("Java");       st.push("Source");       st.push("code");       System.out.println("Top object is: "+st.peek());    } }OutputTop object is: code

What Does the Method Empty Do in Java

Vikyath Ram
Updated on 25-Feb-2020 10:00:29

150 Views

The empty() method is used to test if this stack is or not.Exampleimport java.util.*; public class StackDemo {    public static void main(String args[]) {           Stack st = new Stack();       st.push("Java");       st.push("Source");       st.push("code");       System.out.println("Is stack empty: "+st.empty());    } }OutputIs stack empty: false

Advertisements