Results 1 to 4 of 4

Thread: This is ticking me off

  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.

  2. #2
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: This is ticking me off

    Try taking away these initializations when declaring private data..

    private String itemName = null;
    private String itemDesc = null;
    private int itemBuyPrice = 0;
    private int itemSellPrice = 0;

    changed to

    private String itemName;
    and so on

    I can't test this at work, but let me know if this works.

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: This is ticking me off

    Code:
    	public static void main(String args[]) {
    		Item testItem = new Item();
    		testItem.setItemName("Milk");
    		System.out.println(testItem.getItemName());
    	}
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

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

    Re: This is ticking me off

    Quote Originally Posted by ComputerJy
    Code:
    	public static void main(String args[]) {
    		Item testItem = new Item();
    		testItem.setItemName("Milk");
    		System.out.println(testItem.getItemName());
    	}
    That's what my problem was. I was declaring the variable outside the main code block and instantiating it inside the main loop which caused the error.

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