CSc2310 tutoring session, week 8 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm 11/27/2012 and 11/28/2012 -Class Members vs Instance Members
CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: There are 2 sections: 1.Review -Class Members vs Instance Members 2. Q&A -Answer your questions about java programming
Class Members vs Instance Members Members of a class Class Members (i.e. static members) Variable MembersMethod Members Instance Members Variable MembersMethod Members Relations about “members”
Class Members vs Instance Members Grammar To define a “instance member” – Inside a class, define a variable or a method To define a “class member” – Inside a class, put a static modifier before the data type, and define the variable or the method
Members of a class Class Members Variable MembersMethod Members Instance Members Variable MembersMethod Members Example: public class ClassVariableAndInstanceVariable{ private String instance_v; // each object has one private static int class_v=0; // all objects share the same one public ClassVariableAndInstanceVariable(String s ){ instance_v = s; class_v++; } public void instance_method(){ System.out.println(instance_v + "'s instance method is called"); System.out.println("There are " + class_v + " objects of this ClassVariableAndInstanceVariable"); } public static void class_method(){ System.out.println("There are " + class_v + " objects of this ClassVariableAndInstanceVariable"); } Instance methods can access all types of members Static methods can only access static members
Class Members vs Instance Members How to use static members (class members) and instance members? Instance Members – From an object! Class Members – From a class!
Members of a class Class Members Variable MembersMethod Members Instance Members Variable MembersMethod Members Example: public class ClassVariableAndInstanceVariableTester{ public static void main(String[] args){ ClassVariableAndInstanceVariable o1 = new ClassVariableAndInstanceVariable("o1"); ClassVariableAndInstanceVariable.class_method(); ClassVariableAndInstanceVariable o2 = new ClassVariableAndInstanceVariable("o2"); ClassVariableAndInstanceVariable.class_method(); ClassVariableAndInstanceVariable o3 = new ClassVariableAndInstanceVariable("o3"); ClassVariableAndInstanceVariable.class_method(); o1.instance_method(); o2.instance_method(); o3.instance_method(); } Output: There are 1 objects of this ClassVariableAndInstanceVariable There are 2 objects of this ClassVariableAndInstanceVariable There are 3 objects of this ClassVariableAndInstanceVariable o1's instance method is called o2's instance method is called o3's instance method is called
An Example in you class – CourseAverage2 public class DisplayText2{ private static Graphics g; // Class variable public static void main(String[] args){ // Create drawable frame DrawableFrame df = new DrawableFrame("Display Text"); df.show(); df.setSize(210, 85); // Obtain graphics context g = df.getGraphicsContext(); // Display "Monospaced Bold" displayFont(Color.red, "Monospaced", Font.BOLD, "Monospaced Bold", 25); // Display "SansSerif Italic" displayFont(Color.green, "SansSerif", Font.ITALIC, "SansSerif Italic", 50); // Display "Serif Plain" displayFont(Color.blue, "Serif", Font.PLAIN, "Serif Plain", 75); // Repaint frame df.repaint(); } private static void displayFont(Color c, String fontName, int fontStyle, String message, int y){ g.setColor(c); g.setFont(new Font(fontName, fontStyle, 20)); g.drawString(message, 15, y); } It illustrates how to use static members, but it is not a very good example since not too much advantage is obtained by doing so.
An Example in you class – DisplayText2 private static double readProgramScores() { double programTotal = 0.0; for (int i = 1; i <= NUM_PROGRAMS; i++) programTotal += readDouble("Enter Program " + i + " score: "); return programTotal; } private static double readQuizScores() { double quizTotal = 0.0; for (int i = 1; i <= NUM_QUIZZES; i++) quizTotal += readDouble("Enter Quiz " + i + " score: "); return quizTotal; } private static double readDouble(String prompt) { SimpleIO.prompt(prompt); String userInput = SimpleIO.readLine(); return Convert.toDouble(userInput); } // Constants private static final int NUM_PROGRAMS = 8; private static final int NUM_QUIZZES = 5; private static final int MAX_PROG_SCORE = 20; private static final int MAX_QUIZ_SCORE = 10; private static final double PROGRAM_WEIGHT =.30; private static final double QUIZ_WEIGHT =.10; private static final double TEST_WEIGHT =.15; private static final double FINAL_EXAM_WEIGHT =.30; Using static members to finish the task
Now you know how to use static members. However, here is the warning from Haydon: Minimize the number of them, i.e. use them only when there is really a huge advantage. The shared variables (all static variables are shared by all objects) will exponentially increase the time of debugging.
Please let me know your questions. I will be here till 8:30pm