GamerMax5
Jun 26th, 2007, 07:32 PM
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:
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:
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.
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:
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:
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.