Chapter 8 Classes, Declaration and Scope Class vs. Object public vs. private public: accessible both inside and outside the class private: accessible only within the defining class Examples •private •private FramedOval head, leftEye, rightEye, mouth; •private void resetGame() Chase.java •public •public void move(double dx, double dy) •public boolean contains(Location pt) FunnyFace.java Guidelines •Instance variables should always be private •Method should be private if they are helpers --otherwise public Variables •Instance variables •Parameters •Local variables Instance Variables •Visible throughout class in which declared •Should be private •Provided with default value if not initialized Parameters •Visible only in constructors and methods in which declared •May not be declared public or private •Associated with actual parameter during method invocation •Removed after method execution Local Variables • • • • Visible only in methods in which declared May not be declared public or private Removed after method execution Must be given a value before use Static • Shared by all objects of the class • public static final designates a constant ex: public static final Math.PI Color.RED
© Copyright 2026 Paperzz