1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.

Slides:



Advertisements
Similar presentations
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Advertisements

Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Inheritance Inheritance Reserved word protected Reserved word super
1 Nested and Inner classes Reading for these lectures: Weiss, Section 15.1 (Iterators and nested classes), Section 15.2 (Iterators and inner classes).
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
Access to Names Namespaces, Scopes, Access privileges.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
1 Inner classes Reading for these lectures: Weiss, Section 15.1 (Iterators and nested classes), Section 15.2 (Iterators and inner classes). A nested class.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Introduction to Objects A way to create our own types.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Chapter 8: Inner Classes. You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized.
© 2006 Pearson Addison-Wesley. All rights reserved How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Lecture 15.1 Static Methods and Variables Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.
Methods We write methods in our programs for many reasons:
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
Nested Classes CompSci 230 S Software Construction.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Chapter 5: Enhancing Classes
Programming Logic and Design Seventh Edition
Some Eclipse shortcuts
CompSci 230 S Programming Techniques
Lecture 10: More on Methods and Scope
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 16-Nov-18.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Sit next to someone. Today, we do some work in pairs.
CHAPTER 6 GENERAL-PURPOSE METHODS
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Sit next to someone. Today, we do some work in pairs.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
class PrintOnetoTen { public static void main(String args[]) {
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Local variables and how to recognize them
Review of Previous Lesson
A Methodical Approach to Methods
Presentation transcript:

1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably caused because people can’t explain what a class is. We go back to basics here; then the concepts of nested, inner, and anonymous classes will become clearer. Memorize the following statements!!!!! A class is a drawer of a file cabinet. The drawer contains (1) the static entities defined in the class and (2) all created instances of the class. Each instance of the class contains the nonstatic entities declared in the class. If you can remember this, you can figure out how nested classes work yourself.

2 What’s a class: Examples public class C { public static int x; public int y; public static void p1(int b) {…} public int p2(int b) {…} } public class M { public static void main(String[] pars) { C d= new C(); C e= new C(); } main contents of M’s drawer x p1 contents of C’s drawer (after exec.) a0 y p2 a1 y p2 C C

3 About drawing contents of drawers for classes We don’t always draw the instance of a class in the class drawer, simply because it is sometimes to messy to do, but we know they are there. The name of the class in the upper right corner of the instance reminds us where it belongs. Below is how we might draw the diagram on the previous page. In what we do next, always remember! A class is a file drawer that contains static entities that are defined in the class and created instances of the class. Each instance contains all the nonstatic entities that are defined in the class. main contents of M’s drawer x p1 contents of C’s drawer a0 y p2 C a1 y p2 C

4 Nested class (static class defined in another class) public class M { public static void main(String[] pars) { C d= new C(); C e= new C(); } public static class C { public static int x; public int y; public static void p1(int b) {…} public int p2(int b) {…} } Where does the nested class go? Follow the rules! main contents of M’s drawer x p1 contents of C’s drawer a1 y p2 C C a0 y p2 C

5 Nested class (static class defined in another class) public class M { public static void main(String[] pars) { C d= new C(); C e= new C(); } public static class C { public static int x; public int y; public static void p1(int b) {…} public int p2(int b) {…} } On previous slide, a file drawer is drawn within another file drawer; physically, that’s hard to do, and it’s also messy to draw as more and more nested files are used. So we invent a flattened way to represent the situation that allows us to eliminate nested file drawers (next slide).

6 Flattened view of nested class public class M { public static void main(String[] pars) { C d= new C(); C e= new C(); } public static class C { public static int x; public int y; public static void p1(int b) {…} public int p2(int b) {…} } C’s file drawer is named M$C. C is static, so put variable C with value M$C in M’s drawer --give it “primitive type” class. Place a “scope box” in upper right of M$C’s drawer, which contains the name of the class file-drawer in which it is defined. (Java uses M$1 instead of M$C.) a0 y p2 M$C a1 y p2 M$C main M’s drawer C M$C class p1 M$C’s drawer x M scope box

7 Why use a static class defined in another class? public class M { public static void main(String[] pars) { C d= new C(); C e= new C(); } public static class C { public static int x; public int y; public static void p1(int b) {…} public int p2(int b) {…} } Reasons for defining nested class C in class M 1. Allows C to reference a static entity in M using just its name (e.g. main, and not M.main). 2. Better reason. For organizational purposes. (a) Can make C private, so other parts of program can’t see it. That’s good if they don’t need to see it. (b) Reduce the number of files in program that the programmer has to deal with. (c) Keep logically related things together.

8 Inner class: a NON-static class C defined in class M public class M { private static int x= 0; private int y= 0; private void p(int q) { C c= new C(); C d= new C();} private class C { public int z= 0; public void p1(int b) { p(b+x+y+z); }; } Questions: Where do we place x? Where do we place y and p and C? So how many instances of class C are there? Where do we place z and p1? What’s the flattened view? infinite execution. Don’t run program.

9 Inner class: non-static class C defined in class M public class M { private static int x= 0; private int y= 0; private void p(int q) { C c= new C(); C d= new C();} private class C { public int z= 0; public void p1(int b) { p(b+x+y+z); }; } That’s all there is at the beginning, because everything else belongs in instances of M. We’ll add a method main to class M. M’s drawer x 0

10 public class M {private static int x= 1; public static void main(String args[]) { M m= new M(); m.p(5); } private int y= 2; public void p(int q) { C c= new C(); c.p1(5); C d= new C(); d.p1(6); } private class C { public int z= 3; public void p1(int b) {System.out.println("In p1: " +b+z+y+x);}; } Consider call a1.p1(5) (a1 is name of object). Body of p1 can reference (1) parameter b (2) p1 and z of instance a1 (3) y and p of the instance of M in which a1 occurs (4) static components x and main of M. Situation just before exec. of body of main M’s drawer x 1main (return address) mainM m null args

11 Inner class: non-static class C defined in class M public class M {private static int x= 1; public static void main(String args[]) { M m= new M(); m.p(5); } private int y= 2; public void p(int q) { C c= new C(); c.p1(5); C d= new C(); d.p1(6); } private class C { public int z= 3; public void p1(int b) {System.out.println("In p1: " +b+z+y+x);}; } } Flattened situation after exec. of m= new M(); mainM m a0 args M’s drawer x 1 main a0$C’s drawer a0 y 2 p M C a0$C scope box contains name of instance in which drawer belongs

12 Inner class: non-static class C defined in class M public class M {private static int x= 1; public static void main(String args[]) { M m= new M(); m.p(5); L1: } private int y= 2; public void p(int q) { C c= new C(); c.p1(5); C d= new C(); d.p1(6); } private class C { public int z= 3; public void p1(int b) {System.out.println("In p1: " +b+z+y+x);}; } } Situation before method body for call m.p(5) a0$C’s drawer mainM m a0 args M’s drawer x 1main a0 M y 2 pC a0$C pa0 q 5 L1 c null d null

13 Inner class: non-static class C defined in class M public class M {private static int x= 1; public static void main(String args[]) { M m= new M(); m.p(5); L1: } private int y= 2; public void p(int q) { C c= new C(); c.p1(5); L2: C d= new C(); d.p1(6); } private class C { public int z= 3; public void p1(int b) {System.out.println("In p1: " +b+z+y+x);}; } } Situation after c= new C(); a0$C’s drawer mainM m a0 args M’s drawer x 1main a0 M y 2 pC a0$C pa0 q 5 L1 c a1 d null a0 a1 z 3 p1

14 Inner class: non-static class C defined in class M public class M {private static int x= 1; public static void main(String args[]) { M m= new M(); m.p(5); L1: } private int y= 2; public void p(int q) { C c= new C(); c.p1(5); L2: C d= new C(); d.p1(6); } private class C { public int z= 3; public void p1(int b) {System.out.println("In p1: " +b+z+y+x);}; } } Situation after c= new C(); a0$C’s drawer mainM m a0 args M’s drawer x 1main a0 M y 2 pC a0$C pa0 q 5 L1 c a1 d null a0 a1 z 3 p1

15 Inner class: non-static class C defined in class M Situation during call c.p1(5), just before method body is executed. What can be referenced in method body? (1) in frame: b (2) using frame’s scope box: in a1: z and p1 (3) using a1’s class, what is in drawer a0$C (4) using drawer a0$C’s scope box, what is in a0: y, p, and C (5) Using a0’s class, what is in drawer M: x and main a0$C’s drawer mainM m a0 args a0 M’s drawer x 1main M a0 y 2 pC a0$C pa0 q 5 L1 c a1 d null a0$C a1 z 3 p1 a1 L2 b 5

16 Can also define a class within a method public class M { private static int x= 1; public static void main(String args[]) { M m= new M(); m.p(5); } private int y= 2; public void p(int q) { int w= 4; class C { public int z= 3; public void p1(int b) {System.out.println("In p1: " +b+z+y+x);}; }; C c= new C(); c.p1(5); L2: C d= new C(); d.p1(6); } You yourself can figure out how this works, what it means, by following the rules. Class C is a local variable of method p, so it exists only in a frame for a call on p and disappears, along with the frame, when call is completed. The value of C is its file drawer, inside the frame; but you can figure out what a flattened view would be.