classdemojava.txt - Author CS307 Course Staff Date Description Demos constructors static vs instance methods and method overloading public class

classdemojava.txt - Author CS307 Course Staff Date...

This preview shows page 1 - 2 out of 2 pages.

/******************************************************************/ /* Author: CS307 Course Staff */ /* Date: February 14, 2005 */ /* Description: Demos constructors, static vs instance methods, */ /* and method overloading. */ /******************************************************************/ public class DemoClass { private int x; public DemoClass() { // assign default value x = 0; } public DemoClass(int x) { // use this.x to refer to the instance variable x // use x to refer to a local variable x (more specifically, // method parameter x) this.x = x; } public DemoClass(DemoClass otherDemo) { // copy the value from the otherDemo this.x = otherDemo.x; } // static method (aka class method) public static void s1() { return; } // instance method public void i1() { return; } // static calling static OK // static calling instance is a compile-time error public static void s2() { // i1(); // compile-time error s1(); // DemoClass.s1 return; } // instance calling static OK // instance calling instance OK public void i2() { s1(); // DemoClass.s1();
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

Unformatted text preview: i1(); // this.i1(); return; } // call various versions of overload() based on their // list of parameters (aka function signatures) public void overloadTester() { System.out.println("overloadTester:\n"); overload((byte)1); overload((short)1); overload(1); overload(1L); overload(1.0f); overload(1.0); overload('1'); overload(true); } public void overload(byte b) { System.out.println("byte"); } public void overload(short s) { System.out.println("short"); } public void overload(int i) { System.out.println("int"); } public void overload(long l) { System.out.println("long"); } public void overload(float f) { System.out.println("float"); } public void overload(double d) { System.out.println("double"); } public void overload(char c) { System.out.println("char"); } public void overload(boolean b) { System.out.println("boolean"); } public static void main(String args) { DemoClass dc = new DemoClass(); dc.overloadTester(); } } // end of
View Full Document

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors