Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.

Similar presentations


Presentation on theme: "Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance."— Presentation transcript:

1 Basic Class Structure

2 Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance methods to access that data object – a particular instance of a class –purpose is to encapsulate data –created via the new operator –many objects of a given class may exist –each object will have the same instance data, with different values –all objects will share the same instance methods –an object is required to invoke instance methods MyClass mc = new MyClass(); mc.someMethod();

3 Class –variables (data) –methods (behavior) variables (local to the method) statements –Declarations –assignments –method invocations –change of control »tests »loops »return »break »continue »try/catch blocks

4 Variables Must be declared with a type: –primitive type: int x; –class type: String s; Declaration of a variable does not create an object of that type: Rectangle r; r = new Rectangle (10, 10, 50, 100); Scope: variables have meaning within the statement block {} where they are declared (including inner blocks) public class-level variables have meaning outside the class: –System.out –Dimension dim = this.getSize() int width = dim.width;

5 Methods Method Definition public class BankAccount { private double balance; // makes deposit & returns the new balance public double deposit (double amount) { balance = balance + amount; return balance; } Method Invocation public class Test { public static void main (String[] args) { BankAccount account = new BankAccount (1000); double value = account.deposit (100.); }

6 Constructor Sets up the object: initializes instance variables Similar to a method, but –no return type –same name as class Definition public class BankAccount { private double balance; public BankAccount (double amount) { balance = amount; } Invocation BankAccount account = new BankAccount (1000.);

7 Static Elements a class may have variables and/or methods that are not associated with an object these are called class or static members only one copy of a static variable exists, no matter how many objects are created they are identified by the static keyword they are referenced by using the class name instead of an object instance –setBackground (Color.red); // red is a static variable –double d = Double.parseDouble (“2.5”); main is a static method which is called by the java interpreter –public static void main (String[] args)


Download ppt "Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance."

Similar presentations


Ads by Google