1 / 8

More on Classes

More on Classes. Pepper With help from http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html. This inside the class code that has an instance. Refers to the instance of the class that is running the code right now

lloyd
Download Presentation

More on Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Classes Pepper With help from http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

  2. This inside the class code that has an instance Refers to the instance of the class that is running the code right now Static methods have no “this” because they run the code from the Class, itself http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

  3. Using Objects (such as in your main routine) • First create a variable to hold an instance • MyClass x ; • Then fill it with an instance by calling constructor • x = new MyClass(1); • Access its public variables with : • x.myvar1 • Access its methods: • x.myMethod(‘a’);

  4. Encapsulating • Why? When other programmers use your class, you must not change anything public or you will break their code. • Ex: Fang – if we have a game using advance(), and Fang upgrades to insist on advance(int y), all our code breaks • How? • Public – everyone • Private – no one • Protected – extended can • Nothing - your package (your bluej panel) can • Method or Variable http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

  5. Class Variable • Static – just like method static – one per class • Ex: Class variable total # of bikes • static intnumbBikes = 0; (defaulting to 0) • each bike instance points to that one variable # of bikes. • Can change if your program asks it to • numbBikes = numbBikes + 1; • Changes the total number for all the bike instances , not just the one you are accessing. • Without static – a separate value for each instance • Ex: speed, color of a bike • int speed = 0; • Constants • Make it unchangeable with the word final after static • Capitalize by convention • ex: maximum number of bikes: • static final int MAXBIKES = 15; • Cannot later have MAXBIKES = MAXBIKES+1; • http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

  6. Exercises Do these exercises, but your deck of cards should only have the face cards so you have less coding. JQKA http://docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html

  7. Exercise 2 Do question #1 and Exercise 1 & 2. (Skip the garbage collection questions.) http://docs.oracle.com/javase/tutorial/java/javaOO/QandE/objects-questions.html

  8. More on Arrays – For Each • Shortcut for loop through an array: • for (variable to hold value : array name) • Ex: int[] arr= {1,2,3,4};int tot = 0;for (int x : arr){ tot = tot + x;}System.out.println(tot);

More Related