[java] why can't i use functions in main?

Started by
7 comments, last by Alpha_ProgDes 20 years, 1 month ago
this be my code

import java.io.*;

public class SMarket {
    
    public static void main (String[] args) {
        java.io.BufferedReader stndin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
        System.out.print("Please enter number of shares you wish to buy: ");
        stndin.readLine();
        System.out.println();
        int r_price =  10 + (int)(Math.random() * 6);
        System.out.println(r_price);
        
        buyStock();
    }
    
    public void buyStock () {
        
    }
}
i thought it was ok to have function belonging to the class in main. do i have to provide a constructor for it to work? if i do, could someone tell me the reason? any case you''re wondering these are the errors i get:

D:\program\JCreatorV3_LE\MyProjects\Queue_LList\SMarket.java:13: non-static meth
od buyStock() cannot be referenced from a static context
        buyStock();
        ^
D:\program\JCreatorV3_LE\MyProjects\Queue_LList\SMarket.java:8: unreported excep
tion java.io.IOException; must be caught or declared to be thrown
        stndin.readLine();
                       ^
2 errors
Press any key to continue...
 

Beginner in Game Development?  Read here. And read here.

 

Advertisement
You can only call static methods in main iirc.

--------------------
Though this program be madness, yet there is a method in''t
--------------------Though this program be madness, yet there is a method in't
quote:Original post by Alpha_ProgDes
D:\program\JCreatorV3_LE\MyProjects\Queue_LList\SMarket.java:13: non-static method buyStock() cannot be referenced from a static context        buyStock();        ^ 
Either make buyStick static or add a constructor, create an instance and then call buyStock on that instance.

quote:
D:\program\JCreatorV3_LE\MyProjects\Queue_LList\SMarket.java:8: unreported exception java.io.IOException; must be caught or declared to be thrown        stndin.readLine();                       ^ 
You''re missing a try-catch[-finally] block for exception handling.

You may want to check out the Java tutorials at Sun''s website. Exceptions are integral to Java programming.
buyStock() is a standard member function, so it needs to have an object to work with. Either create an SMarket object in main() and call buyStock() on that, or make buyStock() a static method.
"non-static method buyStock() cannot be referenced from a static context"

Your buyStock() function requires an object to operate on. main() is a static function, and as such provides none.

buyStock(); implies this->buyStock(); (or whatever equivalent in Java), but main() provides no 'this' pointer/reference/whatever.


The other is an exception checking error, deal with it yourself

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on March 7, 2004 4:22:15 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
there are two different kind of methods, instance and static. Instance methods can only be called from an object
ex:
class A {    public static void main (String[] args) {        A obj = new A ();        obj.method ();    }    void method ()  {]}


while static methods are called without an object using only the class name
A.main (args) 

since static methods doesn''t have an object context instance methods can''t be called from them. Thats why your code doesn''t work. You need to create an instance of your class or change your method to be static
humanity will always be slaved by its ignorance
thanks, a plenty.

though i''ve now learned that code snippets are dangerously misleading even if they are from textbooks

can''t understand why they would show IO expressions/statement without the appropriate try and catch blocks....

in any case, solved. thanks again!

Beginner in Game Development?  Read here. And read here.

 

quote:Original post by Alpha_ProgDes
this be my code
import java.io.*;public class SMarket {        public static void main (String[] args) {        java.io.BufferedReader stndin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));        System.out.print("Please enter number of shares you wish to buy: ");        stndin.readLine();        System.out.println();        int r_price =  10 + (int)(Math.random() * 6);        System.out.println(r_price);                buyStock();    }        public void buyStock () {            }}



It''s not wrong, per se, however since you already imported java.io.*, there is no reason to type java.io.BufferedReader in = new java.io.BufferedReader(...);

  BufferedReader in = new BufferedReader(new InputStreamReader(System.in))); 

Should suffice, you''re typing enough, don''t make any more work =)


Project ARPEG: Product, Darkness SeigeThe seige begins...
Or you could just declare the main method as throwing an IOException

public static void main (String[] args) throws IOException {        java.io.BufferedReader stndin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));        System.out.print("Please enter number of shares you wish to buy: ");        stndin.readLine();        System.out.println();        int r_price =  10 + (int)(Math.random() * 6);        System.out.println(r_price);                buyStock();    }
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.

This topic is closed to new replies.

Advertisement