• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I compile this type of code?  RSS feed

 
Chris Weathers
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to define a class seperate from the main program. Do I put the class in the same file or what? Compiler doesn't like it in the same file and I can't get it to import on its own either.



[ May 10, 2005: Message edited by: Chris Weathers ]
[ May 10, 2005: Message edited by: Chris Weathers ]
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

First of all, you can only have one public class in a file, therefore one solution would be to remove public before class MyObject (assuming you are working in MyProgram.java)

Another solution would be to move MyObject into MyObject.java and save the file in the same directory as MyProgram.java (or anywhere in your classpath).
 
Adisesha Rao Raya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If you want to use more than one class in one file, specify the class that you want to give the name same as file name (which contains main()) as public specifier.Give other class names with out public specifier.If you provide public specifier with class name the compiler forces you give only one class in one file.
In your example remove public specifier to your MyObject class. Then it will run fine.

Regards
Seshu
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

The compiler doesnt like it because one file can have only one 'public' class. and the file name should correspond to the public class name.
A file may contain any number of classes you want, but only one public class.

yes, you can put the class in another file in the same directory(not going into CLASSPATH yet) and compile them.

make sure you compile your MyObject class before compiling the MyProgram class. Because if the MyObject class is not compiled, the compiler will whine about 'not finding MyObject class'.

matter of 5 mins!!! wow! now this is a popular post!!!
[ May 10, 2005: Message edited by: Neeraj Dheer ]
 
Chris Weathers
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all...

I changed it to below and it works...but I have another question...if I leave off "public" before the class, what is the class permission default? Is it defaulting to private?

 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The scope will be between private and protected. It will be visible to members of the package, but not outside the package.
 
Chris Weathers
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks...say I want to have MyObject in a separate file in the same directory. I compile MyObject.java and then "import MyObject.*;" inside MyProgam.java, correct?
[ May 10, 2005: Message edited by: Chris Weathers ]
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no. you dont need to do that if MyObject is in the same directory.

when you do

import MyObject.*;

what you are saying is that import ALL classes declared in the package MyObject. in your case, MyObject is a class, not a package, so the import statement is not valid.

if your only concern is using MyObject inside MyProgram, then you dont have to do anything as long as MyObject is in the same directory as MyProgram or, as one of the above posts, somewhere in your CLASSPATH
 
Chris Weathers
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhh, I get it now, thanks!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!