Import a method from a neighboring Java class
Introduction
Example
Suppose there is a package com.andrei.homeworkIt contains the files App.java and TestType.java
TestType.java
can print the type of an object to the screen with the method test().
We need to call this method from
App.java
First file TestType.java does not interest us much, but let us make sure it has the method test()
package com.andrei.homework; public class TestType { public static void test(Object value) { int varD = 1.1; test(varD); // Convert double to int test(int(varD)); } public static void test(Object value) { if (value instanceof Integer) { System.out.println("This is Integer"); } else if (value instanceof String) { System.out.println("This is String"); } else if (value instanceof Float) { System.out.println("This is Float"); } else if (value instanceof Double) { System.out.println("This is Double"); } } }
Second file App.java
package com.andrei.homework; import static com.andrei.homework.TestType.test; public class App { public static void App() { double varD = 2.5; test(varD); } }
This is Double
Article author: Andrei Olegovich