1 / 32

Programming with Classes Class Methods & Variables Week 11

Programming with Classes Class Methods & Variables Week 11. Programming with Objects. CONCEPTS COVERED THIS WEEK. Class Variables Class Methods Running Java outside of BlueJ The main Method. Programming with Classes. CLASS VARIABLES.

Télécharger la présentation

Programming with Classes Class Methods & Variables Week 11

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. Programming with Classes Class Methods & Variables Week 11

  2. Programming with Objects CONCEPTS COVERED THIS WEEK • Class Variables • Class Methods • Running Java outside of BlueJ • The main Method

  3. Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number?

  4. Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number? First Idea: Create an int field called number and add 1 to it every time the object constructor is called. public class Triangle { // instance variables private int number; ... // Triangle constructor public Triangle() { ... number++; System.out.println("Triangle object " + number + " created"); }

  5. Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number? First Idea: Create an int field called number and add 1 to it every time the object constructor is called. This does not work as number is an instance variable and therefore re-initialised to zero each time a Triangle is instantiated/created

  6. Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number? Second Idea: We need to create a non-instance count that can be incremented after each new Triangle, and assign its current value to the instance variable number. This is precisely the purpose behind the creation of so-called static, or Class, variables Class variables hold state that is shared in common among all objects of the class.

  7. Programming with Classes CLASS VARIABLES public class Triangle { // class variable private static int count; // instance variables private int number; ... // Triangle constructor public Triangle() { ... count++; number = count; System.out.println("Triangle object " + number + " created"); }

  8. Programming with Classes CLASS VARIABLES How can we give each Triangle a unique object number? Second Idea: Create a static variable called count and add 1 to it each time the constructor is called. Assign the value of count to an instance variable called number in the constructor. This does work as count is not re-initialised to zero each time a new Triangle object is created

  9. Programming with Classes Triangle triangle1: Triangle triangle3: Triangle count 3 number number 3 1 height height 30 30 width width 40 40 color color “green” “green” triangle2: Triangle number 2 height 30 width 40 color “green” CLASS VARIABLES is instance of… is instance of… is instance of…

  10. Programming with Classes CLASS CONSTANTS Constants are class variables that are given a fixed value using the keyword final For example: In a class that represents bouncing balls it might be useful to declare a constant for the acceleration due to gravity as follows: private static final int gravity = 3; There is a Flash demo of bouncing balls at: http://funwithstuff.com/dswmedia/bouncing_ball_v3.html

  11. Programming with Classes CLASS CONSTANTS

  12. Programming with Classes CLASS METHODS We could use an object method but this would require that an object had already been created and we had its reference. How can we access the value of a class variable? Also, why should we have to create an object to reference a class variable? It seems rather odd to create a BouncingBall object in order to find the value of gravity that will effect ALLBouncing Balls.

  13. Programming with Classes CLASS METHODS How can we access the value of a class variable? A much better solution would be to have a method that related to the BouncingBall class, rather than to a specific BouncingBall object, as this method has no interest in any specific object’s state. Such methods are called Class Methods(they are also often referred to as Static Methods), and they are defined as part of a class definition in the same way as object methods.

  14. Programming with Classes CLASS METHODS public class Triangle { private static int count; // class variable private int number; // instance variable // Triangle constructor public Triangle() { count++; number = count; } // get number of Triangle objects created public static int getCount() { return count; }

  15. A look at class methods/variables in the Triangle class BlueJ Demonstration

  16. Programming with Classes CLASS VARIABLES AND METHODS • Class variables hold state that is shared in common among all objects of class • Class methods act on attributes of the whole class through class variables • Class methods can’t access instance variables

  17. Using Java without BlueJ

  18. Programming with BlueJ The BlueJ directory structure X:\projects\wk10\clock-display\ project: clock-display bluej.pkg bluej.pkh ClockDisplay.java ClockDisplay.class ClockDisplay.ctxt NumberDisplay.java NumberDisplay.class NumberDisplay.ctxt ClockDisplay NumberDisplay

  19. Programming with BlueJ The BlueJ file structure bluej.pkg contains information about classes in the package. One per package. bluej.pkh backup of the package file. Class.java standard Java source code file (text). One per class. Class.class standard Java bytecode file. One per class Class.ctxt BlueJ context file. Contains extra information for a class. One per class.

  20. Programming without BlueJ Standard Java Files Class.java Java source files contain the source code in readable form, as typed in by the programmer. Class.class Java class files contain byte code (a machine readable version of the class). They are generated by the compiler from the source file. Where Class is the name of the Java class being defined/created

  21. Programming without BlueJ Standard Java Edit-Compile-Run Cycle 011010 110101 1001 10 source file class file 011010 110101 010001 1 1 1 0111 0110110 compiler (javac) virtual machine(java) editor

  22. Programming without BlueJ Editing Java Source Files • A Java file can be edited using any text editor e.g. Notepad • Careful with using Word as, by default, Word does not save in text format • Be sure to save changes before compiling!

  23. Programming without BlueJ Command Line Invocation • Compilation and execution of Java in JDK are done from a command line. • Windows: DOS shell, i.e. Command Prompt: • Ensure that the directory for the Java compiler (javac) and Java runtime (java) is set in the command path e.g. • set path=C:\Program Files\Java\jdk1.6.0_03\bin

  24. Programming without BlueJ Java Compilation • The name of the JDK compiler is javac • To compile a Java class definition:javac<source name> • javac compiles the source file as well as all other classes the source file depends upon. • Example usage of javac:C:\> cd X:\projects\wk10\ X:\projects\wk10\> javac Triangle.java

  25. Programming without BlueJ Java Compilation (Error Messages) Example usage of javac: C:\> CD X:\projects\wk07\ X:\projects\wk07\> javac Triangle.java Triangle.java:22: ';' expected. private static int count ^ 1 error X:\projects\wk07\> The programmer has to open the Java file in the editor, find the line number, fix the error and recompile.

  26. Programming without BlueJ Java Program Execution Example usage of java: X:\projects\wk07\> java Triangle • “java” starts the Java virtual machine (JVM). • The named class is loaded and execution is started. • Other classes are loaded as needed. • Only possible if class has been compiled.

  27. Programming without BlueJ Java Program Execution – Problem: Execute What? Example usage of java: X:\projects\wk07\> java Triangle Exception in thread "main" java.lang.NoSuchMethodError: main How does the system know which of the methods of the class to execute?

  28. Programming without BlueJ Java Program Execution – The Main Method Example usage of javac: X:\projects\wk07\> java Triangle Exception in thread "main" java.lang.NoSuchMethodError: main The solution: The Java system always executes a class method called main with a specific signature.

  29. Programming without BlueJ Java Program Execution – The Main Method Fixed signature for the main method: public static void main(String[] args){ ...} • For this to work, a main method must exist in one (and only one) of the classes being called! • It is possible to place all of an application’s code in a class with just a main method! • But this is absolutely NOT RECOMMENDED!

  30. Programming without BlueJ Java Program Execution – The Main Method Example of a main method in a class called GameApp: public static void main(String[] args){ Game game = new Game(); game.play(); } • In general the main method should ONLY • create an object • call the first method

  31. Creating a Java program • Writing the Java code • Any editor (such as Notepad) can be used to write a Java program. However, most professional programmers use a software application called an Integrated Development Environment or IDE • JCreator • http://www.jcreator.com/

  32. Writing with JCreator

More Related