Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Alpha_ProgDes

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

This topic is 5279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...
 

Share this post


Link to post
Share on other sites
Advertisement
You can only call static methods in main iirc.

--------------------
Though this program be madness, yet there is a method in''t

Share this post


Link to post
Share on other sites
quote:
Original post by Alpha_ProgDes

D:\program\JCreatorV3_LE\MyProjects\Queue_LList\SMarket.java:13: non-static meth
od 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 excep
tion 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
"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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 =)


Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!