Although String objects are not mutable, the classes that you create will have mutable objects. If the reference parameter is for a mutable object, the method could change the actual object. However, it is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification. Methods can even access the private data and methods of a parameter that is a reference to an object if the parameter is the same type as the method’s enclosing class. Note that Strings are immutable objects, so they cannot be changed by the method; only a new changed copy of them can be made.
Methods can also return values of any type back to the calling method. The calling method should do something with this return value, like printing it out or saving it in a variable. Try the problems below to practice with a String method that takes a parameter and returns a boolean value.
class ClassName { // static variable public static type variableName; // static method public static returnType methodName(parameters) { // implementation not shown } } // To call a static method or variable, use the Class Name System.out.println(ClassName.staticVariable); ClassName.staticMethod();
Static methods only have access to other static variables and static methods. Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), and static methods cannot call non-static methods. However, non-static methods have access to all variables (instance or static) and methods (static or non-static) in the class.
class ClassName { // static variable public static type variableName; // static method public static returnType methodName(parameters) { // implementation not shown } } // To call a static method or variable, use the Class Name System.out.println(ClassName.staticVariable); ClassName.staticMethod();
Static methods only have access to other static variables and static methods. Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), and static methods cannot call non-static methods. However, non-static methods have access to all variables (instance or static) and methods (static or non-static) in the class.
Since there is only 1 copy of a static variable or method, static variables are often used to count how many objects are generated. In the following class Person, there is a static variable called personCounter that is incremented each time the Person constructor is called to initialize a new Person object. The static method printCounter() prints out its value. You can also watch how it works in the Java visualizer by clicking the CodeLens button below.
The keyword this can be used in a class to refer to the current calling object.
For example, in the following Class Person, when we create an object p1 and call the constructor or p1.setEmail(), the word “this” refers to p1. And when we make the same method calls with object p2, “this” refers to p2. Run the code below and also check it out in the Java visualizer with the Code Lens button which shows how this refers to different objects when the code is run.
Static methods cannot refer to this or instance variables because they are called with the classname, not an object, so there is no this object.
The keyword this is sometimes used by programmers to distinguish between variables. Programmers can give the parameter variables the same names as the instance variables and this can distinguish them and avoid a naming conflict. For example, both the instance variable and the parameter variable are called name in the code below.
// instance variables private String name; // constructor public Person(String name) { // Set this object's instance variable name to the parameter variable name this.name = name; }
The this variable can be used anywhere you would use an object variable. You can even pass it to another method as an argument. Consider the classes below, Pay and Overtime. The Pay class declares an Overtime object and passes in this (the current Pay object) to its constructor which computes the overtime with respect to that Pay object. Try this code in the Java visualizer. Here is an image that shows how this and myPay and p all refer to the same object in memory.