110 likes | 216 Vues
Join Haidong (Haydon) Xue for a comprehensive tutoring session on Class Members vs Instance Members in Java, scheduled for November 27 and 28, 2012, from 5:30 PM to 8:30 PM. This session will cover key concepts such as the definitions and differences between class members and instance members, and provide practical examples to strengthen your understanding. Enhance your Java programming skills with a Q&A segment where you can ask your questions directly. Visit our [website](http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/) for further details.
E N D
CSc2310 tutoring session, week 8Fall, 2012 • Class Members vs Instance Members Haidong(Haydon) Xue 5:30pm—8:30pm 11/27/2012 and 11/28/2012
CSc2310 Tutoring • Time: 5:30pm-8:30pm • Tutor: HaidongXue • Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections: • Review • Class Members vs Instance Members 2. Q&A • Answer your questions about java programming
Class Members vs Instance 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
Example: public class ClassVariableAndInstanceVariable{ private String instance_v; // each object has one private static intclass_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!
Example: public class ClassVariableAndInstanceVariableTester{ public static void main(String[] args){ ClassVariableAndInstanceVariableo1 = new ClassVariableAndInstanceVariable("o1"); ClassVariableAndInstanceVariable.class_method(); ClassVariableAndInstanceVariableo2 = new ClassVariableAndInstanceVariable("o2"); ClassVariableAndInstanceVariable.class_method(); ClassVariableAndInstanceVariableo3 = 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 Examplein you class – CourseAverage2 publicclassDisplayText2{ privatestatic Graphics g; // Class variable publicstaticvoid main(String[] args){ // Create drawable frame DrawableFramedf = newDrawableFrame("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(); } privatestaticvoiddisplayFont(Color c, String fontName, intfontStyle, String message, int y){ g.setColor(c); g.setFont(newFont(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 Examplein you class – DisplayText2 Using static members to finish the task privatestaticdoublereadProgramScores() { doubleprogramTotal = 0.0; for (int i = 1; i <= NUM_PROGRAMS; i++) programTotal += readDouble("Enter Program " + i + " score: "); returnprogramTotal; } privatestaticdoublereadQuizScores() { doublequizTotal = 0.0; for (inti = 1; i <= NUM_QUIZZES; i++) quizTotal += readDouble("Enter Quiz " + i + " score: "); returnquizTotal; } privatestaticdoublereadDouble(String prompt) { SimpleIO.prompt(prompt); String userInput = SimpleIO.readLine(); returnConvert.toDouble(userInput); } // Constants privatestaticfinalintNUM_PROGRAMS = 8; privatestaticfinalintNUM_QUIZZES = 5; privatestaticfinalintMAX_PROG_SCORE = 20; privatestaticfinalintMAX_QUIZ_SCORE = 10; privatestaticfinaldoublePROGRAM_WEIGHT = .30; privatestaticfinaldoubleQUIZ_WEIGHT = .10; privatestaticfinaldoubleTEST_WEIGHT = .15; privatestaticfinaldoubleFINAL_EXAM_WEIGHT = .30;
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