Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Help in program

  Asked By: Lambodar    Date: Nov 06    Category: Java    Views: 714
  

Here is my problem i hava two classes in two different files


Main.java
---------
//Forget about import

public class Main
{
public Main()
{
Ball smallBall = new Ball()
}

public static void main(String[] args)
{
Main demo = new Main()
}
}


Ball.java
---------

public class Ball
{
public Ball()
{
System.out.println("Ball Class");
}
}



Now this is just an example made as simply as possible
My problem is that when i try to compile Main.java, where in the
constructor Ball object is created compiler gives me an error that
class Ball doesnt exists even though both files are in the same folder.

Please help me, i am working on a large project and have like 10
classes and i cannot dividem into different files because of above issue

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Erin Dunn     Answered On: Nov 06

You need to create a package for your project  and include the package
declaration in all the files  used in the project

Main.java: First line should be
package samplePackage;
and use the same package declaration in all the files.

 
Answer #2    Answered By: Ana Bradley     Answered On: Nov 06

Should be problem  with ur classpath. Set the calsspath to '.;'. And try it. It
should work.

 
Answer #3    Answered By: Bernard Gutierrez     Answered On: Nov 06

it is too simple.
compile the 2 files  togethe. just like : javac Main.java Ball.java

by the way: you had better to use an ide (JCreator , Jbuilder, etc...) to help
you to compile.

 
Answer #4    Answered By: Vilhelm Fischer     Answered On: Nov 06

Main.java depends on Ball.java in your program. So,
compile Ball.java first and get its class  file
Ball.class. Then compile  Main.java. It should work.

 
Didn't find what you were looking for? Find more on Help in program Or get search suggestion and latest updates.




Tagged: