• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting error identifier expected  RSS feed

 
Florian Muraczewski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone,

I am pretty new to Java and I am playing around a little bit.

I created a superclass Ships and under that a class CivilShips. Under that HumanBulkFreighter.

I declared variables in Ships and CivilShips and wanted to have them set in HBF to a specific value. When I know try to compile them I get the following:



Can someone kick me in the right direction?

Many thanks.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

We need to see the code the errors relate to be able to give accurate help.
Please post the code (remembering to UseCodeTags).
 
Florian Muraczewski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the freighter:



here the ships:



and last civilships:



thanks again.

Sorry that I didn´t posted it at the beginning
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the HumanBulkFreighter class you can't set those values other than in a constructor or method body. My guess is you want to default constructor for that class with those lines of code in it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Florian Muraczewski wrote:here is the freighter:thanks again.

So what are the types of these fields? The compiler expects a field to be defined as
[type] [name] = [value];
so it's assumed that the first thing its sees is a TYPE (even though it's not), and therefore assumes that you haven't given it a name (or 'identifier').

HIH

Winston
 
Florian Muraczewski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I thought because I get those variables from ships class I can set them there.

How can I set them without a method or declare them new? Because in ships I have a function which uses two variables. I thought when I create a new object of hbf it would use the new variables. Or should i remove the standard variables from ships, but then the function won´t work or?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the variables are available, but you can't put statements like those assignments outside of method bodies or constructors, like Tony said. Using a constructor seems to the way to go in this case.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:So what are the types of these fields? The compiler expects a field to be defined as
[type] [name] = [value];

The values are defined, but they are defined in the Ships class.

Florian Muraczewski wrote:ok, I thought because I get those variables from ships class I can set them there.

You can (provided the classes are in the same package), but you can't use them as you are currently trying to do, see my previous post.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The values are defined, but they are defined in the Ships class.

Actually, they aren't all defined; hence my question.

@Florian: I suspect that you need to re-read the section of your book, or class notes, or the relevant section of the tutorials on how to set up subclasses and initialise their values properly, because what you're trying to do just won't fly.

Winston
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, they aren't all defined

I suspect the missing fields are in the CivilShips class (which wasn't posted). In that case the only problem is that assignments (as opposed to declarations) can't live outside of methods or constructors.
 
Florian Muraczewski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok will read that again.. and again, man why is that so complicated to start..

Thanks for the tipps.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Florian Muraczewski wrote:ok will read that again.. and again, man why is that so complicated to start..

Because programming ain't simple man.

Winston
 
Florian Muraczewski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a heads up. I rewrote it and now it looks better. I need to read a lot more, but at least I am now able to use superclass and subclass
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Florian Muraczewski wrote:Just as a heads up. I rewrote it and now it looks better. I need to read a lot more, but at least I am now able to use superclass and subclass

Well done! Hope we were of some help.

Winston
 
Florian Muraczewski
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you were a big help pointing me to where I have to look for the error.

I will now work through the tutorials, maybe I will be bale to get something going
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!