Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaring variables

 
jessie mccarty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the source file the same, but im still getting the same error =(
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
jessie mccarty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jessie mccarty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


so the code will be then like this?
still getting an error " class or interface expected"
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i separate them but still error " class or interface expected"
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, post the exact error message. it should refer to the line where the error occurs.
 
jessie mccarty
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic