Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes declaring variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "declaring variables" Watch "declaring variables" New topic
Author

declaring variables

jessie mccarty
Greenhorn

Joined: Dec 03, 2005
Posts: 8
i need help plsss

i have ths code



and for some reason im getting an error stating

class ClassTwo is public, should be declare in the file name classTwo.java

any ideas.??? did i already declare it right?
Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
Well you already have one class taht is public make the ClassTwo package.
But i should suggest that you put the class ClassTwo in a new file makees it more clear. .

regards Rille
[ December 03, 2005: Message edited by: Rikard Qvarforth ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You are only allowed to have one public class in a source file, and the name of the source file must match the public class name.
jessie mccarty
Greenhorn

Joined: Dec 03, 2005
Posts: 8
I made the source file the same, but im still getting the same error =(
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11357
    
  16

just to be clear...

you have TWO separate files, one named "sample.java". this file should only have the code for the sample class.

you have a second file, named "ClassTwo.java" (and yes, the case DOES matter for both these filenames).

is this correct?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
jessie mccarty
Greenhorn

Joined: Dec 03, 2005
Posts: 8
so do i need to take it out..actually what im trying to get form this is the result.. this is an exercise that i need to modify to get thr right result.. so what should i do?

thanks much
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by jessie mccarty:
so do i need to take it out...

Yes, if both class definitions are public, then they each need to be in their own separate file, just as fred described above.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
jessie mccarty
Greenhorn

Joined: Dec 03, 2005
Posts: 8


so the code will be then like this?
still getting an error " class or interface expected"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by jessie mccarty:
...so the code will be then like this? ...

Not quite. That looks like the class definition for "sample" with its name changed to TestTwo. Basically, you just need to separate what you had in your original post...

Note: By convention, class names are capitalized -- e.g., Sample.
jessie mccarty
Greenhorn

Joined: Dec 03, 2005
Posts: 8
i separate them but still error " class or interface expected"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by jessie mccarty:
i separate them but still error " class or interface expected"

Can you post your current code? The code in my examples compile and run fine.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11357
    
  16

also, post the exact error message. it should refer to the line where the error occurs.
jessie mccarty
Greenhorn

Joined: Dec 03, 2005
Posts: 8
soory for the late respond...actually the codes are compiling, when i tried to execute it, it gives me "Exception in main "thred" java.lang.NoSuchMethodError": main"
what does it mean?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14196
    
  20

It means you don't have your classpath setup correctly.
Try running your program with:

java -cp . TestTwo

See the following tutorial for a step by step explanation: First Steps for Microsoft Windows


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
GeeCON Prague 2014
 
subject: declaring variables