1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

JAVA problem

Discussion in 'BBS Hangout' started by Luckyazn, Mar 26, 2010.

Tags:
  1. Luckyazn

    Luckyazn Member

    Joined:
    Jun 23, 2003
    Messages:
    4,375
    Likes Received:
    68
    Can someone help me with this JAVA problem:

    Given
    public class Q7_10 {
    private boolean x;
    int y;
    static String s;
    void imethod() {}
    }

    Which of the following statements are incorrect?
    Q7_10 f= new Q7_10();

    a) System.out.println(f.y);
    b) Q7_10.smethod();
    c) Q7_10.imethod();
    d) System.out.println(Q7_10.s);
    c) System.out.println(Q7_10.x);



    I'm done with all other problems but stuck on this one. Hoping for some explanation would be helpful too.

    I hate taking programming language and information classes online since there's no one teaching you, it seems like you're reading and trying to learn yourself. (only course offer)


    thanks,
     
  2. FrancisFan

    FrancisFan Member

    Joined:
    Jul 22, 2000
    Messages:
    1,166
    Likes Received:
    32
    Q7_10.smethod(); - There is no smethod

    Q7_10.imethod(); - The imethod is not static

    System.out.println(Q7_10.x); - x is not static and if it was static its private variable
     
  3. Luckyazn

    Luckyazn Member

    Joined:
    Jun 23, 2003
    Messages:
    4,375
    Likes Received:
    68
    Hey FrancisFan,

    thanks alot.

    So there were 3 wrong answers
     
  4. Agent94

    Agent94 Member

    Joined:
    Jun 28, 2002
    Messages:
    3,643
    Likes Received:
    4,117
    a) Maybe wrong. I would consider it bad code. Since it is not given a scope, it is package scope. Only classes in the same package can access the variable.

    b) Wrong. There is no smethod.

    c) Wrong. It is trying to call imethod statically and imethod is not declared static.

    d) Maybe wrong. It can access the variable statically since it is declared static. However only classes in the same package can access the variable.

    e) Wrong. It is trying to access a not static variable statically.

    B, C, and E are definitely wrong. A and B are wrong if the class calling them is in a different package. Overall its a pretty poor example.
     
  5. Agent94

    Agent94 Member

    Joined:
    Jun 28, 2002
    Messages:
    3,643
    Likes Received:
    4,117
    Fixed
     

Share This Page