• No se han encontrado resultados

2. Perspectivas Conceptuales de la interacción social en el proceso de

2.3 La intersubjetividad y el proceso de formación de la conciencia en la

2.3.1 El proceso de emergencia de la mente (espíritu) y del sí mismo

Static methods

You can call static members with the name of the class, and that also applies to the static methods. You can call static methods with the name of the class as well as the name of the objects.

There are some important rules, which apply to static methods.

1. The keyword this can't be used inside static methods because this is associated with the current instance.

2. You can not have instance variables within a static method because the instance probably does not exist at the time that the method is called.

An example of a static method is the following method, which returns the sum of four integers. This method is static because there is no need to create an object to calculate the sum of four integers. In order to call this method, we use the name of the class.

Example 2

public class Calculate {

public static int getSum(int i, int i2, int i3, int i4) {

return i + i2 + i3 + i4;

}

public static void main(String[] args) {

int x = getSum(5, 4, 6, 2);

// or

// int x = Calculate.getSum(5, 4, 6, 2);

System.out.print(x);

} }

The previous example writes 17 to the standard output.

Quiz 1: Comparing static variables with instance variables

What happens when the following program is compiled and run?

public class Employee {

int nr; // instance variable static int stNr; // class variable

public Employee() {

nr++;

stNr++;

}

public static void main(String[] args) {

Employee emp1 = new Employee();

Employee emp2 = new Employee();

Employee emp3 = new Employee();

System.out.print(Employee.stNr + ", ");

System.out.print(emp1.nr + ", ");

System.out.print(emp2.nr + ", ");

System.out.print(emp3.nr);

} }

Select the correct answer:

a. This code writes "3, 1, 2, 3" to the standard output.

b. This code writes ”3, 1, 1, 1” to the standard output.

c. This code writes "1, 1, 1, 1" to the standard output.

d. This code writes "3, 3, 3, 3" to the standard output.

e. This code writes nothing to the standard output.

Explanation

The no-argument constructor is called each time we create an employee object.

We have created 3 employee objects and by creating an object the statement stNr++ adds one to the static variable. After creating the three objects stNr = 3.

For all the instances the stNr remains the same because it is static.

Usually, you don't need to use the name of objects to access static members. You can also use the name of the class.

The instance variable nr belongs to the objects. The value of the instance variable nr for all the objects is equal to one.

The correct answer is b.

Assignments

1. Create two more employee objects namely; emp4 and emp5.

2. Does the instantiation of the two previous objects affect the static variable stNr?

3. Compile and run the program to test your expectation.

Quiz 2: Java static members

What happens when the following program is compiled and run?

public class MyClass {

static int x = 3;

public MyClass() {

x++;

}

public static int method(int i, int i2) {

x += (i - i2);

return x;

}

public static void main(String[] args) {

MyClass mc1 = new MyClass();

MyClass mc2 = new MyClass();

System.out.print(MyClass.x + ", ");

MyClass mc3 = new MyClass();

MyClass.method(8, 3);

System.out.print(MyClass.x);

} }

Select the correct answer:

a. This code writes "0, 0" to the standard output.

b. This code writes "5, 5" to the standard output.

c. This code writes "11, 11" to the standard output.

d. This code writes "5, 11" to the standard output.

e. This code writes "3, 3" to the standard output.

Explanation

The no-argument constructor is called each time we create a MyClass object.

By instantiating the objects mc1 and mc2 the no-argument constructor adds each time 1 to the

value of x which is initially equal to 3.

x = 3 + 1 + 1 = 5.

By instantiating the object mc3 the no-argument constructor adds one more to the value of x.

x = 5 + 1 = 6.

The statement MyClass.method(8,3); invokes the method, which adds (i - i2) to the value of x.

x = 6 + (8 - 3) = 11.

The correct answer is d.

Assignments

1. Write a method called resetX to reset the value of the variable x to any integer number you wish.

2. Test your method.

Quiz 3: Class variables and instance variables

What happens when the following program is compiled and run?

public class MyClass {

static int x = 6;

int y = 3;

MyClass() {

x += 3;

y += 2;

}

void method(int i) {

this.y = y - i;

x++;

}

public static void main(String[] args) {

MyClass mc1 = new MyClass();

MyClass mc2 = new MyClass();

MyClass mc3 = new MyClass();

mc1.method(3);

System.out.print(MyClass.x + ", " + mc1.y);

} }

Select the correct answer:

a. This code writes "16, 6" to the standard output.

b. This code writes "16, 2" to the standard output.

c. This code writes "12, 2" to the standard output.

d. This code writes "13, 5" to the standard output.

e. This code writes "15, 5" to the standard output.

Explanation

The statement MyClass mc1 = new MyClass(); creates the object mc1.

By creating an object, the program calls the no-argument constructor.

It adds 3 to the value of x and 2 to the value of y.

x = 6 + 3 = 9. x is static and it belongs to the class.

y = 3 = 2 = 5. y is an instance variable and its value belongs to the object mc1.

The statement MyClass mc2 = new MyClass(); creates the object mc2.

It calls the no-argument constructor and adds more 3 to the value of x.

x = 9 + 3 =12. The value of y of the object mc1 remains the same.

The statement MyClass mc3 = new MyClass(); creates the object mc3.

It calls the no-argument constructor and adds more 3 to the value of x.

x = 12 + 3 = 15.

The statement mc1.method(3); invokes the method, which adds one to the value of x.

x = 15 + 1 = 16.

By invoking the method this.y = (y - i)

The value of the variable y of the object mc1 is equal to 5.

y = 5 - i = 5 - 3 = 2.

The correct answer is b.

Assignments

1. Create five object references of the class MyClass namely mc4, mc5, mc6, mc7 and mc8.

2. Does that affect the static variable x?

3. Test your code.

Quiz 4: Static variables and methods

What happens when the following program is compiled and run?

public class MyClass {

static int x = 2;

MyClass() {

x += 4;

}

static void methodA(int i) {

x = x - i;

}

int methodB(int i) {

return x + i;

}

public static void main(String[] args) {

MyClass mc1 = new MyClass();

MyClass.methodA(2);

MyClass mc2 = new MyClass();

System.out.print(mc2.methodB(3));

} }

Select the correct answer:

a. This code writes "11" to the standard output.

b. This code writes "12" to the standard output.

c. This code writes "14" to the standard output.

d. This code writes "7" to the standard output.

e. This code writes "9" to the standard output.

Explanation

The statement MyClass mc1 = new MyClass(); calls the no-argument constructor,

which adds 4 to the value of x.

x = 2 + 4 = 6.

The statement MyClass.methodA(2); invokes the methodA.

x = x - i = 6 - 2 = 4.

The statement MyClass mc2 = new MyClass(); calls the no-argument constructor, which adds 4 to the value of x.

x = 4 + 4 = 8.

The statement System.out.print(mc2.methodB(3)); invokes the methodB.

x = x + i = 8 + 3 = 11.

The correct answer is a.

Assignments

1. What is the result if you replace the statement x += 4; with the statement x ++

2. Compile and run the program to test the result.

Quiz 5: A static StringBuffer example

What happens when the following program is compiled and run?

public class MyClass {

static int x;

static StringBuffer sb = new StringBuffer();

public MyClass() {

myMethod();

}

public void myMethod() {

x += 3;

sb.append(x);

}

public static void main(String[] args) {

MyClass mc = new MyClass();

MyClass mc2 = new MyClass();

MyClass mc3 = new MyClass();

System.out.println(MyClass.sb);

} }

Select the correct answer:

a. This code writes "333" to the standard output.

b. This code writes "0" to the standard output.

c. This code writes "3" to the standard output.

d. This code writes "369" to the standard output.

e. This code writes "18" to the standard output.

Explanation

The statement MyClass mc = new MyClass(), creates an object, which calls the MyClass no-argument constructor.

The constructor invokes the method myMethod().

The statement x += 3 increments the value of x by 3, x = 0 + 3 = 3.

The statement sb.appends(x), appends the string representation of the x argument, which is 3.

By creating the second object “mc2” the x += 3 increments the value of x by 3, x = 3 + 3 = 6.

The statement sb.appends(x), appends the string representation of the x argument, which is 6.

By creating the third object “mc3” the x += 3 increments the value of x by 3, x = 6 + 3 = 9.

The statement sb.appends(x), appends the string representation of the x argument, which is 9.

Remember that x and sb are both static, and that is why both of them belong to the class. The value of x and sb are the same for all the objects.

The correct answer is d.

Assignments

1. Remove the keyword static from the class variable x . 2. Does the previous step affect the program?

3. Compile and run the program to check your expectation.

4. Remove both static keywords from the class variables x and sb .

5. Do you think that the previous step changes the result of the execution of the program?

6. Compile and run the program to check your expectation.

Assignment chapter 10: Track the names of programming languages in a string

1. Create a new class ProgrammingLanguage.

2. Declare an integer variable numberOfLanguages and a String variable language inside the class.

3. Instantiate the following five objects of the class ProgrammingLanguage as follows: Java , C++ , Python , PHP and Ruby .

4. Each time an object is created, the variable language keeps the track of the name of the objects as follows: Java , C++ , Python , PHP , Ruby .

5. The variable numberOfLanguages should also keep track the number of the created objects.

6. Compile and run your program to test your code.

Use the operator += to keep track of the variable language of all the created objects, as follows :

String language = "".

language += "Java".

language += "C++".. etc.