Found 4338 Articles for Java 8

How to load classes at runtime from a folder or Java package

Abhinaya
Updated on 04-Feb-2020 11:06:03

669 Views

Using CLASSPATH, you can load any classes at runtime.Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as −\sources\com\apple\computers\Dell.java \classes\com\apple\computers\Dell.classBy doing this, it is possible to give access to the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the ... Read More

How to use sub-package in Java?

Sravani S
Updated on 04-Feb-2020 11:12:22

880 Views

Subpackages are similar to sub-directories. Consider an example. The company had a com.apple.computers package that contained a Dell.java source file, it would be contained in a series of subdirectories like this −....\com\apple\computers\Dell.javaAt the time of compilation, the compiler creates a different output file for each class, interface, and enumeration defined in it. The base name of the output file is the name of the type, and its extension is .class.For example −// File Name:Dell.java package com.apple.computers; public class Dell { } class Ups { }Now, compile this file as follows using -d option −$javac -d.Dell.javaThe files will be compiled as ... Read More

How to use classes in other package in Java

Ramu Prasad
Updated on 04-Feb-2020 11:21:21

533 Views

You can understand it using an example where a Boss class is defined in payroll package.package payroll; public class Boss {    public void payEmployee(Employee e) {       e.mailCheck();    } }if the Employee class is not in the payroll package? The Boss class must then use one of the following techniques for referring to a class in a different package.The fully qualified name of the class can be used. For example −payroll.EmployeeThe package can be imported using the import keyword and the wildcard (*). For example −import payroll.*;The class itself can be imported using the import keyword. ... Read More

How to access Java package from another package

Smita Kapse
Updated on 04-Feb-2020 11:26:52

5K+ Views

You can understand it using an example where a Boss class is defined in payroll package.package payroll; public class Boss {    public void payEmployee(Employee e) {       e.mailCheck();    } }if the Employee class is not in the payroll package? The Boss class must then use one of the following techniques for referring to a class in a different package.The fully qualified name of the class can be used. For example −payroll.EmployeeThe package can be imported using the import keyword and the wild card (*). For example −import payroll.*;The class itself can be imported using the import ... Read More

How to run Java package program

Ankitha Reddy
Updated on 26-Oct-2023 02:32:02

27K+ Views

Let us look at an example that creates a package called animals. It is a good practice to use names of packages with lower case letters to avoid any conflicts with the names of classes and interfaces.Following package example contains interface named animals −/* File name : Animal.java */ package animals; interface Animal {    public void eat();    public void travel(); }Now, let us implement the above interface in the same package animals −package animals; /* File name : MammalInt.java */ public class MammalInt implements Animal {    public void eat() {       System.out.println("Mammal eats");    } ... Read More

How to compile packages in Java

Nikitha N
Updated on 04-Feb-2020 10:47:31

4K+ Views

Let us look at an example that creates a package called animals. It is a good practice to use names of packages with lower case letters to avoid any conflicts with the names of classes and interfaces.Following package example contains interface named animals −/* File name : Animal.java */ package animals; interface Animal {    public void eat();    public void travel(); }Now, let us implement the above interface in the same package animals −package animals; /* File name : MammalInt.java */ public class MammalInt implements Animal {    public void eat() {       System.out.println("Mammal eats");    } ... Read More

Java constructor return a value but, what?

Priya Pallavi
Updated on 04-Feb-2020 10:54:52

753 Views

No. Java constructor cannot return a value. If required, just create a method which calls the required constructor and returns the required value. See the example below.public class Tester {    public Tester(){}    public static Tester getInstance(){       Tester tester = new Tester();        return tester;    } }

How to find package explorer in Java eclipse project?

V Jyothi
Updated on 20-Feb-2020 05:19:59

20K+ Views

To view the project explorer, click on Window menu then, click on Show View and select Project Explorer.There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.

How to convert a list collection into a dictionary in Java?

Nikitha N
Updated on 30-Jul-2019 22:30:21

1K+ Views

Following is an example to convert a list collection into a dictionary in Java.Example Live Demoimport java.util.ArrayList; import java.util.Dictionary; import java.util.Hashtable; public class CollectionDictionary {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       System.out.println(list);       Dictionary dictionary = new Hashtable();       Hashtable hashTable = new Hashtable();       hashTable.put(1, list.get(0));       hashTable.put(2, list.get(1));       hashTable.put(3, list.get(2));       hashTable.put(4, list.get(3));       System.out.println(hashTable);    } }Output[JavaFx, Java, WebGL, OpenCV] {4=OpenCV, 3=WebGL, 2=Java, 1=JavaFx}

How to copy or clone a Java ArrayList?

Abhinanda Shri
Updated on 25-Feb-2020 09:50:14

1K+ Views

The clone() method of the java.util.ArrayList class returns a shallow copy of this ArrayList instance (i.e the elements themselves are not copied). Using this method, you can copy the contents of one array list to other.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String args[]) {       ArrayList arrlist1 = new ArrayList();       arrlist1.add(new StringBuilder("Learning-"));       ArrayList arrlist2 = (ArrayList) arrlist1.clone();       StringBuilder strbuilder = arrlist1.get(0);       strbuilder.append("list1, list2-both pointing to the same StringBuilder");       System.out.println("The 1st list prints: ");       for (int i = ... Read More

Advertisements