Download presentation
Presentation is loading. Please wait.
Published byHelen Stanley Modified over 9 years ago
1
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
2
CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections: 1.Review -Class Members vs Instance Members 2. Q&A -Answer your questions about java programming
3
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”
4
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
5
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
6
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!
7
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
8
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.
9
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
10
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.
11
Please let me know your questions. I will be here till 8:30pm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.