Results 1 to 10 of 10
  1. #1
    jonsamwell is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

    Default 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?? 
                
            }
        }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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();
           }
        }
    }

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by jonsamwell View Post
    What am i doing wrong?
    you need StockItem to be the outer class.
    Proper style says it should be stored in a file named StockItem.java

  4. #4
    jonsamwell is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

    Default

    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?

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    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();
        }
    }

  6. #6
    jonsamwell is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

    Default

    cool got that

    so i can't call this funciton from another *.java class then?

    say main.java?

  7. #7
    Rooneyz is offline Member
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    25
    Rep Power
    0

    Default

    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

  8. #8
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    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.

  9. #9
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by udayadas View Post
    Just replace the line which is giving compiler error with the following....
    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

  10. #10
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    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

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Dynamic method calling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:46 AM
  3. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 03:38 AM
  4. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 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
  •