tags:

views:

185

answers:

6

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?

A: 

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

   }
}

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

+5  A: 

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;

}
Feet
You answered the quetsion! If the helper class is in the same package as the facade class I can just say private bar myBar; and I get me a new bar?
Ziggy
If you wish to instantiate the class, you will need to use the constructor. myBar = new Bar(); You may wish to find some Java tutorials on the net to get you started.
Feet
In this particular example the import is redundant as the classes are in the same package.
Dan Dyer
A: 

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

javac file1.java file2.java file3.java
java file1
Alexander K
+1  A: 

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).

Jon Skeet
+1  A: 

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
    }
}
miceuz
+1  A: 

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.

Vincent Ramdhanie