1 / 11

CSc2310 tutoring session, week 8 Fall, 2012

CSc2310 tutoring session, week 8 Fall, 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: Haidong Xue Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/ There are 2 sections:

maren
Download Presentation

CSc2310 tutoring session, week 8 Fall, 2012

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. 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

  2. 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

  3. Class Members vs Instance 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. 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

  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. 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

  8. 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.

  9. 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;

  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

More Related