开发者

How do you add classes to a main method

开发者 https://www.devze.com 2023-01-15 19:47 出处:网络
I have three .开发者_JS百科java files and I need to get them to work together.I think that I need to add all the classes to a main method but I am not sure if this is correct and if I just add the nam

I have three .开发者_JS百科java files and I need to get them to work together. I think that I need to add all the classes to a main method but I am not sure if this is correct and if I just add the name of the class and the format.

I figured it out, the three files had a package listed at the top of each. I created a new Java project in Eclipse and then a source folder and in the source folder I created a package with the name that they all referenced. Now it runs. Thanks for all of you help for the Eclipse/Java beginner.


You are right: what you think is not right :P

Java can find the classes that you need, you can just use them straight away. I get the feeling that you come from a C/C++ background (like me) and hence think that you will need to "include" the other classes.

java uses the concept of namespaces and classpaths to find classes. Google around for it.

A little example of how variety of classes can be used together:

// A.java

public class A {
    public void sayIt() { sysout("Said it by A!"); }
}

// B.java
public class B {
    public void doIt() { sysout("Done it by B!"); }
}

// MainClass.java

public class MainClass {
    public static void main(String[] args) {
         A aObj = new A();
         B bObj = new B();

         aObj.sayIt();
         bObj.doIt();
    }
}

Note that there are no includes/imports here because all of the classes are in the same namespace. If they were not, then you'd need to import them. I will not add a contrived example for that coz its too much to type, but should google for it. Info should be easy enough to find.

Cheers,
jrh


If they are in the same package you do not need to do anything, as they are automatically imported for you, but otherwise you'll need to add import statements before your class declaration.

Once this is done, you can reference static members directly ie ClassB.staticMethod(); or instantiate the class ie ClassB classb = new ClassB();

But honestly, if you are this confused, you need to spend some more time doing tuturials.

  • http://eclipsetutorial.sourceforge.net/totalbeginner.html
  • http://download.oracle.com/javase/tutorial/getStarted/cupojava/index.html
  • http://www.freejavaguide.com/corejava.htm


I am not sure what you mean by "adding classes to a main method". If you want to make use of several classes inside your Java program, just import the needed classes/packages at the beginning and create an instance of each class as you go along.


I learned this from a beginner program called Jeroo

Basically if I want to create a new "Jeroo", I would write the following on my Main method:

Jeroo Bob = new Jeroo();
{ methods... }

So basically:

[class] [customnameofclass] = new [class]
0

精彩评论

暂无评论...
验证码 换一张
取 消