Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default cant access a print statement from a different class!

    Hi, my code is as follows:


    class A{
    int i = 10;
    int j = 10;


    }

    class B extends A{


    System.out.println(i+j);

    }

    class SimpleInheritance{
    public static void main(String args[]){

    B o = new B();

    System.out.println(o.i+o.j);

    }

    }

    The problem is this gives the following errors:
    SimpleInheritance.java:12: <identifier> expected
    System.out.println(i+j);
    ^
    SimpleInheritance.java:12: <identifier> expected
    System.out.println(i+j);
    ^
    SimpleInheritance.java:12: ';' expected
    System.out.println(i+j);
    ^
    SimpleInheritance.java:12: illegal start of type
    System.out.println(i+j);
    ^
    SimpleInheritance.java:12: <identifier> expected
    System.out.println(i+j);
    ^
    SimpleInheritance.java:12: ';' expected
    System.out.println(i+j);
    ^
    SimpleInheritance.java:24: reached end of file while parsing
    }
    ^
    7 errors


    .. Now if we rewrite the program as :

    class A{
    int i = 10;
    int j = 10;


    }

    class B extends A{

    int show()
    {
    System.out.println(i+j);
    return(1);
    }
    }

    class SimpleInheritance{
    public static void main(String args[]){

    B o = new B();

    System.out.println(o.show());

    }
    }


    It works when we give the print statement inside the method show(). Otherwise it wont work.. Can u tell me why is it so? why cant we access a print statement directly from a different class?

    Please clarify...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: cant access a print statement from a different class!

    Statements need to be in an executable block (usually a method).
    You can NOT just place them anywhere.
    Last edited by Norm; 02-17-2012 at 01:01 PM. Reason: added NOT

Similar Threads

  1. How to access finger print scanner?
    By JunedAhmed in forum Networking
    Replies: 6
    Last Post: 01-28-2011, 12:43 PM
  2. Multiple colors in print-statement
    By schutter07 in forum New To Java
    Replies: 8
    Last Post: 11-20-2010, 11:46 AM
  3. How to format the print statement
    By ftrengnr in forum New To Java
    Replies: 1
    Last Post: 09-18-2010, 10:02 PM
  4. problems with print statement
    By soccer_kid_6 in forum New To Java
    Replies: 8
    Last Post: 03-22-2010, 02:24 PM
  5. how to access and print the dwg files in java
    By vmag in forum Advanced Java
    Replies: 0
    Last Post: 03-19-2010, 06:29 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
  •