• Post Reply Bookmark Topic Watch Topic
  • New Topic

Very Frustrated Java Beginner  RSS feed

 
P Scine
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am a Java beginner and I am very frustrated with my current assignments and this BlueJ book we are using. This book doesn't provide much scope that pertain to the exercises and my instructor only provides very vague responses to my questions. I really want to learn Java, not just satisfy the requirements for my course. This is all leaving a bad taste in my mouth, I need help desperately. This book isn't helping neither is my instructor, that is why I am here. Hopefully someone can help me and I can return the favor to someone else when I learn more.

Currently I am having major problems with chapter 4 of BlueJ. For instance, exercise 4.57.

"Implement the findProduct method. This should look through the collection for a product whose id field matches the ID argument of this method. If a matching product is found, it should be returned as the method's result. If no matching product is found, return null.

When looking for a match, you will nedd to call the get ID method on a product."

This is what the Product class constructor looks like.


The getID method has to be passed to the findProduct method. I don't understand how to call that method to the findProduct Method below. I know it requires an external method call but I have never seen a constructor that starts off with public Product findProduct. These are two different classes and when I call or assign anything to the findProduct(id) it returns in an error because the Product variable id is private. I would really appreciate any help, I have been losing sleep over this.




 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome P Scine CodeRanch . someone help you around here... else i will look this thread after some time ...
 
P Scine
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:welcome P Scine CodeRanch . someone help you around here... else i will look this thread after some time ...


Thank you very much. I wish I would have known about this place five weeks ago. : )

Also, does anyone have any experience with BlueJ? It is very confusing, I am reading Ivan Horton's Beginning Java and it is much easier to understand the concepts in that book but it doesn't follow the structure of the course I am in. I love programming, what little I have done, it would be very demoralizing to fail this class because I don't understand the book.

Thanks again.
 
P Scine
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



non-static method getID() cannot be referenced from a static context.

You are trying to access an instance field or instance method from a static method. That is not allowed.
The instance fields and methods belong to an object, while you have no active object in static methods.
Static methods can only call other static methods in their class (or they need to use an explicit object for the call).
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37253
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error means you can only call getId() on a product instance and not the class Product itself.

Look at what is available to you in the StockManager class. You have a list of the products. This means you can loop through the list and call getId() on each instance as you go through the list. Give it a shot and post back with any questions you encounter while writing the loop or if statement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!