How do I "require" a neighbour class in Java?

In Ruby I have often written a number of small classes, and had a main class organize the little guys to get work done. I do this by writing

require "converter.rb"
require "screenFixer.rb"
.
.
.

at the top of the main class. How do I do this in Java? Is it "import?"

Also, could someone please come up with a better title for this question?


Asked by: Maya529 | Posted: 23-01-2022






Answer 1

If you mean that you are going to write some code that will be using another class. Yes you will need to import those classes using an import statement.

i.e.

package com.boo;

import com.foo.Bar;

public class StackOverflow {

private Bar myBar;

}

Answered by: Darcy993 | Posted: 24-02-2022



Answer 2

You don't import you own *.java files in java. Instead you do like this.

javac file1.java file2.java file3.java
java file1

Answered by: John259 | Posted: 24-02-2022



Answer 3

If these other classes are only going to be used by your 'main' class, there are a couple of options to consider which don't require any imports:

  • You could make them private static nested classes inside your main class.
  • You could make them 'package-access' (the default - no access modifier)

If they're public classes elsewhere, however, you will indeed need an import (or fully qualify them everywhere, but that's horrible).

Answered by: Adelaide137 | Posted: 24-02-2022



Answer 4

you should really read a bit about Object Oriented programming.

you don't organize little guys in bigger guy do get work done.

Classes in hold some data and provide method to manipulate that data in meaninful ways. Data usually are variables of some other Class, you must declare where from do you get those. You do this declaration by specifying import statement.

BTW you should explicitly create a class instance to use it, i.e. invoke class constructor method

package com.foo;
import com.foo.Bar;

public class StackOverflow {

private Bar myBar;
    public StackOverflow() { //this is a constructor of StackOverflow class
        myBar = new Bar(); //i'm invoking constructor of Bar class
    }
}

Answered by: Adelaide544 | Posted: 24-02-2022



Answer 5

If all the classes you write are in the same package you do not need to do anything special to use them. If class A wants the service of class B then in class A you may just instantiate an object of B and invoke the methods of B that you want.

If the classes are in different packages however, or if you are using classes from third parties then you will need an import statement

import package.ClasName;

at the top of the file in which you refer to the class.

Answered by: Kristian664 | Posted: 24-02-2022



Answer 6


public class Example {
   public Example(IConverter converter, IScreenFixer screenFixer) {

   }
}

http://en.wikipedia.org/wiki/Facade_pattern ?

Answered by: Dexter307 | Posted: 24-02-2022



Similar questions

java - Find the nearest neighbour / latitude and longitude

I have a table (DB2 database) with city information and corresponding latitude and longitude and many other info related to the city. My requirement is: Input to my application will be latitude and longitude which may or many not be exact lat and long stored in DB. I need to find the nearest City info from the table with the help of input latitude and longitude. Any help is highly appreciated. Is there a ne...


java - KdTree nearest neighbour search algorithm not working properly

I'm implementing a KdTree in java. I have most of the rest of the program done, but I can't seem to get my nearest neighbour search algorithm to work properly. It always returns the root node's values, no matter what. Here is my code: public Point2D nearest(Point2D p) { if (root == null) //if there are no nodes in the tree return null; else return nearest(p, root, root.point); ...


java - Initial Repaint issues and count neighbour issues

I have been trying to make a simple Game of Life simulation to keep myself busy and have come into two issues. One, my "board" isn't being painted to the JFrame when it is first initialised, but it will once the simulation has started. Two, my countNeighbours code is not working. Or at least the observed output is not what is expected. Here is the code: Interface:- public class UserInterface...


java - Finding the nearest neighbour K-NN

Hey Guys I am having trouble with my code that creates 10 points, assigns a distance for each point between 1 and 500 and then finding the nearest two neighbours for each point. so far i have two classes my PointD class which is responsible for the math and assigning the neighbours and my main class which runs the program and calculates the distance: PointD Class: package points; /** * * @Ashl...


java - 2D KD Tree and Nearest Neighbour Search

I'm currently implementing a KD Tree and nearest neighbour search, following the algorithm described here: http://ldots.org/kdtree/ I have come across a couple of different ways to implement a KD Tree, one in which points are stored in internal nodes, and one in which they are only stored in leaf nodes. As I have a very simple use case (all I need to ...


java - Finding smallest neighbour in a 2D array

I have some code that is supposed to find the smallest of the 8 neighboring cells in a 2D array. When this code runs, the smallest is then moved to, and the code run again in a loop. However when it is run the code ends up giving a stack overflow error as it keeps jumping between two points. This seems to be a logical paradox as if Y < X then X !< Y. So it think it is my code at fault, rather than my logic. Here's my...


Java ECLIPSE Nearest TSP with Nearest Neighbour Algorithm

First question I ask here on the forum, had some use on many questions posted here. I am stuck with my current assignment: I need to make a TSP with a nearest neighbour algorithm and then store the shortest "route" in an array. So far, I came up with this: public class Q2b { int route[] = new int[179];//create route array private int nFacilities; // 179. private int nLocations; // 179. ...


java - K nearest neighbour in a 2d plane

The Question statement is as follows: Find the K closest points to the origin in a 2D plane, given an array containing N points.The output must be in non decreasing order. Solution: I have solved this using comparator and priority queue and my code looks like below: class Point { double x; double y; public Point(double x, double...


java - Nearest neighbour Algorithm

according to the page .. http://www.sanfoundry.com/java-program-implement-traveling-salesman-problem-using-nearest-neighbour-algorithm/ this is his java implementation import java.util.InputMismatchException; import java.util.Scanner; import java.u...


java - computes the neighbour Matrix for all pairs of vertices

I have implemented this function but have not gotten the correct output. The task is to compute the neighbour Matrix for all pairs of vertices for every undirected path from one vertex to all other vertices and update that path which has minimum cost. what am I doing wrong here? static COMP108A2Output neighbourhood(int[][] adjMatrix, int gSize) { COMP108A2Output output = new COMP108A2Output(1, gSize)...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top