Results 1 to 4 of 4

Thread: This is ticking me off

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Resolved This is ticking me off

    I'm no n00b to Java but I've been out of it for such a long time that I forget even the basics.

    I made a class called Item that holds the information about an item (generics, has absolutely nothing to do with a game). Here's the code that I made for it:

    Code:
    public class Item {
    	/********************************************
    	Instance Variables
    	********************************************/
    	private String itemName = null;
    	private String itemDesc = null;
    	private int itemBuyPrice = 0;
    	private int itemSellPrice = 0;
    
    	/********************************************
    	Class Default Constructor
    	********************************************/
    	Item() {
    		itemName = null;
    		itemDesc = null;
    		itemBuyPrice = 0;
    		itemSellPrice = 0;
    	}
    
    	/********************************************
    	Accessor Methods
    	********************************************/
    	public String getItemName() { return itemName; }
    	public String getItemDesc() { return itemDesc; }
    	public int getItemBuyPrice() { return itemBuyPrice; }
    	public int getItemSellPrice() { return itemSellPrice; }
    
    	/********************************************
    	Mutator Methods
    	********************************************/
    	public void setItemName(String _itemName) { itemName = _itemName; }
    	public void setItemDesc(String _itemDesc) { itemDesc = _itemDesc; }
    	public void setItemBuyPrice(int _itemBuyPrice) { itemBuyPrice = _itemBuyPrice; }
    	public void setItemSellPrice(int _itemSellPrice) { itemSellPrice = _itemSellPrice; }
    }
    Which as far as I can remember, this is proper Java coding. The compiler didn't complain about anything at all. Then I wrote a class that contains a main method that simply instantiates the Item class, sets the item's name to Milk, and then is supposed to print the name of the item in the console window. Here is the code that I wrote for that:

    Code:
    public class ItemController {
    	/********************************************
    	Instance Variables
    	********************************************/
    	private Item testItem;
    
    	/********************************************
    	Main Method
    	********************************************/
    	public static void main(String args[]) {
    		testItem = new Item();
    		testItem.setItemName("Milk");
    		System.out.println(testItem.getItemName());
    	}
    }
    The compiler complains about trying to access a non-static method outside of a static context. Now as far as I can remember, the main method in Java is supposed to be static and a static code block inside the already static main method would cause an error. So what am I doing wrong here? Thanks in advance for any help. It's greatly appreciated.
    Last edited by GamerMax5; Jun 27th, 2007 at 03:35 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width