Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classes

 
Sebastien Zerbato
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am new to java and I have to code a class. The demonstrator from my course told me to use another class to create a type that is needed in my class, however, I cannot compile due to errors with the declaration of that class. I have checked the chapter in my java book that relates to class declaring and still don't see the issue.
This is the code:



Thanks in advance.
 
Henry Wong
author
Marshal
Pie
Posts: 20896
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



A couple of obvious issues....

* The "class" keyword is used to declare a class.

* Code are part of initializers, constructors, or methods. Java doesn't just allow code anywhere.

Henry
 
Sebastien Zerbato
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A couple of obvious issues....

* The "class" keyword is used to declare a class.

* Code are part of initializers, constructors, or methods. Java doesn't just allow code anywhere.

Henry"
So what you're sating I should do is put that "public tab" in a different file and call it from this file ?
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastien Zerbato wrote:So what you're sating I should do is put that "public tab" in a different file and call it from this file ?


You will find that whenever somebody on a forum says "So what you're saying is..." the answer is inevitably "No, I didn't say any such thing."

And so it is in this case.
 
Sebastien Zerbato
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, let me rephrase this then, what do you think would solve my problem ? Or do you think that creating a different file for "tab" would work ? (in the end, i am just asking for your opinion, not saying that you said or implied that. But whatever.)
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to say. That code which Henry posted... did you mean it to be a class? (Henry pointed out several problems with it, if it was supposed to be a class.) If you did, then yes you should put it in its own file. But that's only one thing you should do. The other things... well, they were what Henry actually said.
 
Sebastien Zerbato
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thank you (I meant it to be a class)
But I don't see any problem with the actual encoding in the public tab ? Could you point it out to me ?
Thanks in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is Java supposed to know you meant for it to be a class?
 
Sebastien Zerbato
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, now that I look at it, I can see that there was no evidence of it being a class.
Thank you for question though.
 
Campbell Ritchie
Sheriff
Posts: 48431
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are writing too much code before finding mistakes. You should not write more than about 5 lines without compiling it. Then you would have known because you would have got an error saying “class interface or enum expected”.
You have got some spelling errors in what you quoted, so the code won’t compile. You should always use ctrl-C ctrl-V to copy code. You have also some code //commented out. We do not need to see that.
You have some incorrect naming conventions, eg use of underscores_ in_ identifiers_.
You should not use StringTokenizer; click on that link to find out why.
 
Sebastien Zerbato
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everybody, I managed to find my mistakes. for now ;)
 
Campbell Ritchie
Sheriff
Posts: 48431
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what was your mistake?
 
Sebastien Zerbato
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't remember, I got blocked after that, and I realized the way I had approached the problem with was just way too complicated so I started from scratch again. My file now compiles and executes when tested with another program. Thank you for sparing me with your time.
 
Campbell Ritchie
Sheriff
Posts: 48431
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastien Zerbato wrote:I don't remember . . .
What a nuisance. If you don’t remember, you will repeat that mistake.
Thank you for sparing me with your time.
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic