0

i have a Java assignment and I would like to find out if I am currently on the right track...

The problem is that I am to create a new class name Tester and it stated that I do not need any instance variables nor constructors. Besides this class, I have 4 other class which is Product class, Lineitem class, Inventory class and money class.

Next I am to add a method: private static void addTestItems(inventory theInventory).

This method is to allow me to add four different type of items to the inventory. For each, I will need to create a product object and then create a Lineitem object containing that product follow by using a addItem(), a method which i have declared in the Inventory class, to add the product object.

The method is private as it is just a helper for another main method: public static void test() In this method, I will use this method to print out some output stating the product that was added into the inventory.

What I manage to research is that in such class, I do not need to declare instance variable and constructor. Source: Answer #2 in http://www.allinterview.com/showanswers/62694.html.

However, what I would like to know is that if I am able to create a static product object in this method. If not, how should I go about declaring the product object to be added into the Inventory class?

In addition to my curiosity, by not declaring instance variables and constructors and going straight to static methods, does this mean that this is a static class? Does anyone have a good example of a static class or static method in regards or is very similar to what I am facing?

By the way, I am using BlueJ to do my assignment. I would like to thank everyone hwo read or help me with this problem And if you find this question to be a lousy one please comment and let me know how I can improve on it. By the way.

4
  • A "static class" is just a normal class. The fact that there's any other kind is an abomination (that thankfully you don't need to worry about yet). Commented Apr 14, 2012 at 3:37
  • @BrendanLong A "static class" is not just a normal class. class Outer { int x ; static class AStaticClass { } class ANormalClass { void meth ( ) { Outer.this.x=5; } } } Commented Apr 14, 2012 at 4:03
  • @emory - This is just a difference of terminology. By normal classes, I meant top-level. I'm perfectly aware of how inner classes work in Java, and my point was that top-level classes are automatically static classes (and inner classes aren't important to beginners, since they exist only to make code hard to read). Commented Apr 14, 2012 at 4:13
  • hmm...guys...its complicating now...i think i haven learn any inner or outer yet...haha...this part of the assignment is tough to understand. Commented Apr 14, 2012 at 4:25

1 Answer 1

3

Can't you just declare a static variable on the class to hold your inventory?

private static List<inventory> list = new LinkedList<inventory>();

It doesn't need to be in a constructor or a method it can just be your variable declaration. Since your class is static it will only get initialized once.

Sign up to request clarification or add additional context in comments.

1 Comment

The class itself is always static -- it will only be instantiated once because the variable is static.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.