Results 1 to 10 of 10
Thread: Trouble will calling a method
- 08-21-2008, 09:29 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 11
- Rep Power
- 0
Trouble will calling a method
hi,
I'm fairly new to java and have been playing about with arrays, the below code inputs some details in an array then prints them out on screen the problem being i can't call the 'stockitem("a",1,2);' function from the main method.
Netbeans produces the error 'cannot find symbol' when i try to call the 'stockitem' function from the main method.
What am i doing wrong?
Any help will be great as i can't seem to figure it out.
Thanks very much
Jon
Java Code:public class Main { class stockitem { String prod_id; int wieght; int cost; public void displaystockitem() { System.out.println("Product ID: "+prod_id); System.out.println("Wieght: "+wieght); System.out.println("Cost: "+cost); } public stockitem(String p,int w, int c) { //creates stock list stockitem sl[] = new stockitem[3]; sl[0] = new stockitem("product_A", 3, 9); sl[1] = new stockitem("product_B", 1, 4); sl[2] = new stockitem("product_C", 5, 3); sl[3] = new stockitem("product_D", 2, 2); for(int i=0;i<2;i++) sl[i].displaystockitem(); } } public static void main(String[] args) { stockitem("a",1,1); //error here?? } }
- 08-21-2008, 09:41 PM #2
Java Code:public class StockItem { String prod_id; int wieght; int cost; public StockItem(String p,int w, int c) { prod_id = p; wieght = w; cost = c; } public void displayStockItem() { System.out.println("Product ID: "+prod_id); System.out.println("Wieght: "+wieght); System.out.println("Cost: "+cost); } public static void main(String[] args) { //creates stock list StockItem[] sl = new StockItem[4]; sl[0] = new StockItem("product_A", 3, 9); sl[1] = new StockItem("product_B", 1, 4); sl[2] = new StockItem("product_C", 5, 3); sl[3] = new StockItem("product_D", 2, 2); for(int i=0; i < sl.length; i++) { sl[i].displayStockItem(); } } }
- 08-21-2008, 09:48 PM #3
- 08-21-2008, 09:58 PM #4
Member
- Join Date
- Aug 2008
- Posts
- 11
- Rep Power
- 0
Thanks,
So how do i call the class from the main method of the main.java file?
because at the moment nothing it happening its just compiling?
- 08-21-2008, 10:08 PM #5
You need to (1) call the object from the standard main() function and (2) execute the compiled code.
You need something like:
Java Code:public class StockItem { public StockItem (); public doSomething() { System.out.println("Hello world"); } /** * usual shell program entry point * @param args the command line arguments */ public static void main(String[] args) { StockItem si = new StockItem(); si.doSomething(); } }
- 08-21-2008, 10:11 PM #6
Member
- Join Date
- Aug 2008
- Posts
- 11
- Rep Power
- 0
cool got that
so i can't call this funciton from another *.java class then?
say main.java?
- 08-21-2008, 11:57 PM #7
Member
- Join Date
- Aug 2008
- Location
- The Netherlands
- Posts
- 25
- Rep Power
- 0
You certainly can... As long as they are in the same package. When this isn't the case you can also do this but you will need an import statement.
Try to create a second class in the same project en compile both. Then try to access one class from the other one. Let us know if it's working
- 08-22-2008, 10:00 PM #8
Hi fellas,
Just replace the line which is giving compiler error with the following.(on the original post)
stockitem ss=new Main().new stockitem("a",1,1);
Hope compiles good.But I fear for a stack overflow exception in runtime.
- 08-22-2008, 10:05 PM #9
That might work, but its better to use the standard style, which I posted up thread, and is in every Java book that I've ever seen.
Using a common pattern makes it easier for the next programmer to figure out.
We don't want Java to be like AP/L, a write once, throw it away language
- 08-22-2008, 10:16 PM #10
You are right,
Further
public stockitem(String p,int w, int c)
{
//creates stock list
stockitem sl[] = new stockitem[3];
sl[0] = new stockitem("product_A", 3, 9);
sl[1] = new stockitem("product_B", 1, 4);
sl[2] = new stockitem("product_C", 5, 3);
sl[3] = new stockitem("product_D", 2, 2);
This is criminal....
Similar Threads
-
Calling a method in another class
By uncopywritable in forum New To JavaReplies: 9Last Post: 10-22-2012, 04:01 PM -
Dynamic method calling
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:46 AM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM -
Help with Calling a method
By Albert in forum New To JavaReplies: 3Last Post: 07-10-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks