Fields Vs. Parameters Vs. Local Variables

Fields Vs. Parameters Vs. Local Variables

Summary 3 – Variables: Fields vs. Parameters vs. Local variables What is this? Type of Scope – where it is legal to refer Lifetime – when it is legal to refer to Description variable to the variable the variable Associated with objects of the class in The lifetime of the object with which Field The class in which it is defined which the field is defined they are associated Associated with a method; its value is given The body of the method for which it From when its method is called until the Parameter to its method when the method is called is a parameter time when its method is exited Local From the point at which it is declared From the time it is declared to the time Defined within a method variable to the end of its enclosing block that its enclosing block is exited Example (from JavaEyes) public class Eye extends JPanel implements MouseMotionListener { private static final int DEFAULT_RADIUS = 25; protected EyeBall eyeBall; DEFAULT_RADIUS, eyeball, eyeColor and eyeRadius are fields. protected Color eyeColor; Every Eye has these four items of data associated with it (the protected int eyeRadius; DEFAULT_RADIUS is shared by all Eyes – see the Summary on static). ... event is a parameter of the method mouseMoved. public void mouseMoved(MouseEvent event) { Point mousePoint = new Point(event.getX(), event.getY()); this.look(mousePoint); } mousePoint is a local variable of the method mouseMoved. } For further study: o Big Java, section 3.4 Instance Fields, describes fields. o Big Java, section 2.4 Method Parameters and Return Values, describes how parameters are used, and section 3.8 Implicit and Explicit Parameters distinguishes explicit parameters of a method from its implicit parameter. o Big Java, section 3.7 Categories of Variables, discusses lifetime, and section 8.8 Scope discusses scope. o Authors of this summary: David Mutchler. o See also the Summaries on Variables: Primitive Type versus Object Type .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    1 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us