• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Change variable on a specific instance  RSS feed

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can  I Increase the value of a variable on a specific instance of a class?
Here's my code:



Eclipse gives me a error at line 4.
"Cannot make a static reference to the non-static field BrickEntity.hit"

If I change hit to  a static int the error goes away. But then I guess it's not specific to that instance? Any ideas ? Thanks

BrickEntity.java
 
Ranch Hand
Posts: 50
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your specific instance of a class is called "other". So you probably meant to write it as: other.hit++

 
Daniel Andersson
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:Your specific instance of a class is called "other". So you probably meant to write it as: other.hit++


Yeah i tried that first but then I got the error "hit cannot be resolved or is not a field."
 
Adrian Grabowski
Ranch Hand
Posts: 50
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does "Entity" have a "hit" field? If not you need to cast your variable to BrickEntity first.
 
Marshal
Posts: 62231
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielx Anderssonx wrote:How can  I Increase the value of a variable on a specific instance of a class? . . .

The object‑oriented way:-Use of things like instanceof are deviations from the tenets of object‑orientation.

I think you may have design problems if you are using instanceof frequently.
 
Daniel Andersson
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:Does "Entity" have a "hit" field? If not you need to cast your variable to BrickEntity first.


No only BrickEntity got the variable "hit". Ok thanks how do I cast my variable to BrickEntity?
 
Marshal
Posts: 5633
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the answer is below, but I have other concerns:
Concerns:
1) See Campbell Ritchie's post about the object oriented way of doing this.
2) At least create a "getter" for the field hit.  An object should never expose its fields.
3) Using "instanceof" like that is a red flag.  You should look carefully through your to see why you need this.
 
Daniel Andersson
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Well, the answer is below, but I have other concerns:
Concerns:
1) See Campbell Ritchie's post about the object oriented way of doing this.
2) At least create a "getter" for the field hit.  An object should never expose its fields.
3) Using "instanceof" like that is a red flag.  You should look carefully through your to see why you need this.


Ok thanks. I don't know any other way to write it right now. I just copied another game tutorial and they used instanceof. Seems to work for now.

I ran to a new problem. Maybe related to this topic.


When writing if at line 5 eclipse tells me the same thing again. "Cannot make a static reference to the non-static field BrickEntity.hit"
But i dont want a static int. "(BrickEntity) other).hit < 2"  don't seem to work within a if condition.
 
Adrian Grabowski
Ranch Hand
Posts: 50
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code, it should work with "if", probably some typo I guess.

As others have mentioned there are some issues with your code, I know it's fun to get straight into programming but you will keep getting stuck, I would recommend a quick crash course on Java and object-oriented concepts, it will make your learning experience much more enjoyable.
 
Daniel Andersson
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:Post your code, it should work with "if", probably some typo I guess.

As others have mentioned there are some issues with your code, I know it's fun to get straight into programming but you will keep getting stuck, I would recommend a quick crash course on Java and object-oriented concepts, it will make your learning experience much more enjoyable.



If i copy from the first example i got 3 errors.


if i delete one ")"


I get 2 errors:
The operator < is undifiened for the argument type(s)BrickEntiy, int and
hit cannot be resolved or is not a field
 
Knute Snortum
Marshal
Posts: 5633
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that BrickEntity is the class, not an instance of the class.  "other" is what you want, as it is a reference to an object, and an object is an instance of a class.
 
Knute Snortum
Marshal
Posts: 5633
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Count your parentheses.  The structure of an if statement is if ( condition ) { } and the condition you want is ( (BrickEntity) other).hit < 2.
 
Daniel Andersson
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote: Count your parentheses.  The structure of an if statement is if ( condition ) { } and the condition you want is ( (BrickEntity) other).hit < 2.


Thank you sir, that did work! Ok got it.
 
Daniel Andersson
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Well, the answer is below, but I have other concerns:
Concerns:
1) See Campbell Ritchie's post about the object oriented way of doing this.
2) At least create a "getter" for the field hit.  An object should never expose its fields.
3) Using "instanceof" like that is a red flag.  You should look carefully through your to see why you need this.



Why is using "instaceof" considered wrong? I did not understand Ritchie's post about the object oriented way.
Could you show a better objected oriented code to use instead of "instanceof"? Maybe hard to explain, it works for now anyways so I'm happy.
I'm sure i will get why its wrong later on. Most importantly i got the game or assignment done and that's an achievement
 
Sheriff
Posts: 12815
211
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using instanceof in code like this is a red flag because you are circumventing polymorphism as a mechanism for abstraction. It indicates that your design has a flaw. If your Entity object must be treated as a specific subclass, then your code should be written to that specific subclass so that Java can figure out what method it needs to call, not you, the programmer.

The fact that you are accessing a field directly is also a red flag. As someone mentioned before, you should at least have getter/setter methods.

It seems there is something conceptually wrong with your API design. The code you wrote knows too much about the idea of tracking "hits" for BrickEntity. If you are going to code to Entity, then you should limit the code to dealing with ideas that apply to all kinds of Entity objects. Maybe you need to find a more abstract idea, like "do something when you collide with something," e.g. have a method like onCollide() instead, then for the BrickEntity, that method would update the hit field of the object. For other kinds of Entity objects, the onCollide() method can do something else.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!