Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can't call class method from outside main

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't call class method from outside main

    Hi, when I try to compile the following I get the error below. However if I get rid of the two lines of code in the shape class it runs fine.

    I don't understand what's going on - I'm calling the same show() method for both mypoint and mypoint2 objects (creating a mypoint2 object in the shape class). Why does the program work when the show() method is called for the mypoint object, but gives an error the the mypoint2 object?

    Thanks.

    error:

    test.java:35: error: <identifier> expected
    mypoint2.show();

     
    class point{
     
    double x=0;
     
        double y=0;
     
     
     
    public point(double x2,double y2)
     
    {
     
    x=x2;
     
    y=y2;
     
    }
     
     
     
     
     
    public point()
     
    {
     
    x=0;
     
    y=0;
     
    }
     
    public void show()
     
    {
     
    System.out.println("X: "+x+" Y:"+y);
     
    }
     
     
     
    }
     
    class shape{
     
    point mypoint2=new point(76,67); //program won't run with these 2 lines of code, but will work if these 2 lines are deleted
     
    mypoint2.show();
     
    }
     
    public class test{
     
    public static void main(String[] args){
     
    point mypoint=new point(76,7);
     
    System.out.println("x and y of point 1 ");
     
    mypoint.show();
     
    } }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can't call class method from outside main

    test.java:35: error: <identifier> expected
    mypoint2.show();
    The code in the shape (java conventions recommends names like: Shape) class needs to be inside of a method.

    Also posted here: https://www.dreamincode.net/forums/t...-outside-main/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to open one Jframe from main method call
    By ankushpruthi in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 1st, 2013, 05:23 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. Why cant I call on the array I returned in the main method?
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 04:08 PM
  4. How to call a value from a different method to main
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 22nd, 2011, 06:29 PM
  5. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM