Simple explanation of fields in Java - java

Shouldn't the last print statement output "Vince"? I'd like to know why it didn't and the concept behind it.
See code below:
public class Example
{
static String name;
public static void main(String[] args)
{
Example.name = "Vince";
System.out.println(Example.name) // will print "Vince"
name = "Tony";
System.out.println(name); // will still print "Tony"
System.out.println(Example.name); // will print "Tony"
}
}

There are no other symbols called name in scope (e.g. no local variables), so name refers to the same thing as Example.name.
If the code said, for example:
String name; // a local variable with the same name
name = "Tony";
System.out.println(name); // will still print "Tony"
System.out.println(Example.name);
then the last line would print Vince, because then you are assigning Tony to the local variable.

Attempt 2 at answering this question.
name is a static field. static means that this field/attribute is shared amongst all objects of the same type. This means that if I were to make an instance of Example, called it Nancy, and then I did Nancy.name = "Drew", it would mean that System.out.println(name) would now equal "Drew" since static fields are shared across all objects of the same type.
Point is, by having the field be static, it was guaranteed to change to whatever value that Example.name or this.name changed it to, depending on which one was more recent.
link for this keyword - https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
A nice example of this playing out, but a bit differently - https://stackoverflow.com/a/47035964/10118965

Related

Stack variables in Java (newbie)

I am staring to study java and currently I am learning about the classes setters.
I see that the most common way to make a setter is something like this.
class Apple{
private String _name;
//Setters
public void setName(String name){
_name = name;
}
}
I am used to C so this code raises me a question. If I am setting _name = name in a function, after this function is completed and his stack is discarded why does the variable _name still stores the right value? This is confusing because in C if I assig a pointer to another pointer inside a function like this it would probably cause a segmentation fault (since name is a temporary variable).
In Java, you as an user don't have control over the stack as in C/C++ languages.
In addition, all non-primitive data types (int, double, etc.) are stored in the heap. The user is only able to use references or pointers and is not required to perform any kind of memory management, since a mechanism known as garbage collector already frees those instances which haven't any reference to them. Therefore, there are no such thing as pointers in Java, although you can assign a null value to a non-primitive variable: Foo f = null;
Therefore, in Java you could literally behave what this C++ code does:
class Foo {
Foo( int a ) {}
};
void bar( int a ) {
Foo f(a); // f is placed in the stack
}
The only way you can create an instance of Foo in java would be like this:
void bar( int a ) {
Foo f = new Foo(a); /* f is a reference to the new instance
* (placed in heap) */
}
Actually, name is an instance variable and setName() is an instance method. This means that there is a separate copy of name for every object of the class Apple. The setName() method sets the value for the global variable of the object it is called with to that of its argument. So, even after the stack of the method is discarded, the object exists and so does the value of this object's copy of name. See this example below:
class Apple {
private String name; //instance variable
public void setName(String name) {
this.name = name; //same as what you have written
}
//main method
public static void main(String[] args) {
Apple obj = new Apple(); //object created
Apple obj2=new Apple();
obj.setName("Yolo");
obj2.setName("Yay!");
System.out.println(obj.name); //displays obj's copy of name that holds the value "Yolo"
System.out.println(obj2.name); //displays obj2's name
}
}
This displays
Yolo
Yay!
I hope this makes it clear to you.
You are setting the reference of name to the reference of _name so they look in the same spot in memory. Therefore when the instance of setName disappears and the variable name with it the reference stored in _name remains.
Since you set _name to private it can only be accessed inside the class Apple. You cant change it within your main method and that's why you create the set method so it can be changed by outside classes (I.e. Your main method)

What does "void" mean as the return type of a method? [duplicate]

This question already has answers here:
Can someone explain a void return type in Java?
(5 answers)
Closed 6 years ago.
I'm confused about "void",
as it pertains to methods.
I don't know what the distinction between two methods is when one has "void" and another doesn't.
For example, if I do:
Public meth (int amount)
{
amount = initial * interest;
return amount;
}
( not sure if it was right, or even valid, to take the name "amount" and name it the same thing as my formal parameter, but what makes sense here is that you're performing a calculation and returning the result)
Then, if I did something like:
Public void Testing (int array[])
{
//code that would modify the internals of an array
}
Would the second one have no "return" because it's more of a general method, that can be applied to any integer array, while the first one is about doing work on specific variables?
Would also appreciate one or two more examples of when I would or wouldn't be using "void" and "return".
One other thing that seems to confuse me is calling methods.
I know sometimes I'll do something like, for example, using the Testing method above,
Testing(ArrayName);
Other times, it will be like:
NameOfWhateverImApplyingMethodTo.MethodName();
And then there are times when things will be done properly by:
Thing1.MethodName(Thing2);
Which circumstances would I switch the syntax for method calls like this?
Java is case sensitive, so the modifier Public is invalid, use public
You can't define a method as public methodName(int a), only a constructor has this signature, a method must be public void methodName(<signature>) for methods that don't return anything or public <return type> methodName(<signature>) for methods that do.
Void basically means that the method will not return anything.
If you did
String name= "tim";
public void getName(){
return name;
}
This would result in an error, because the getName method is returning a string object called name, but the method declaration is saying I am returning nothing - because it is void.
Instead the method should be :
String name = "tim";
public String getName(){
return name;
}
Now when the method getName() is called it will return a string object "name" with "tim" inside of it :)
You might have void for a set method. So for example
String name = "tim";
public void setName(String newName){
this.name = newName;
}
When this method is called you would use setName("Andy"); and it would set the value of the name variable to be "Andy". Nothing is returned in this method, because it is setting something, but there is no need to send anything back, so we use void on the method declaration.
Hope this helps.
The method that has void as return type does not return anything. For example you want to set a field firstName in your class. You will write a setting method like
public void setFirstName(String n) {
this.firstName = n;
}
As you can see you are just setting a class variable and does not require to return anything.
If you dont use void then you have to provide a return type for method. Like if you wish to write a getter for above variable as:
public String getFirstName() {
return this.firstName;
}
Once you provide a return type, you will have to return a value of that type otherwise your code will not compile.
Calling a method can be done based on where you are calling it from and what modifier is used:
If you are calling the method from the same class then you can simply write firstName = getFirstName()
If you are calling the method from another class then you require object of method's class as qualifier like personObject.getFirstName()
If you are calling a static method then you require class name as qualifier like Person.getFirstName();
Return type is what you get out of it. When you call it, what are you hoping to get back? For instance, if the method gets the average of two numbers, then you're expecting a number back, so the return type will be a number type, like "int" (integer).
You can see what it should be using that logic or by looking in the method for the word return - what comes after return is what is returned, and its type should be declared in the method (e.g. if it says "return 4;" it's returning an int, and should be e.g. public int getFour()
You also asked about e.g. testing() vs testing(word)
I remember having the same difficulty. The distinction between the two also relates to the method declaration line. I'll illustrate.
public String testing(){
return "a word";
}
Calling this method by doing "System.out.println(testing());" should print "a word". Calling this method by doing "System.out.println(testing("a word"));" will give you an issue - this is because when you call testing, it looks at the appropriate method: one in the right class, with the right return type and with the right arguments/parameters. If you're calling testing("a word"), that means you're using a String as an argument (because "a word" is a string), and so it tries to use the testing(String aString) method - which doesn't exist.
So you use empty brackets when the method takes no input, and you put stuff in brackets when the method expects stuff. This should be less confusing than it sounds, because it's usually logical - if you want to call a method that returns an average, you need to ask yourself "Average of what?" You'd probably need to supply it with the values you want the average of.
Moving on: (a) testing() versus(b) AClass.testing() versus(c) aclass.testing() -
In (a), there's no class specified. Therefore, if you call it from that class, Java can guess which class: this one, and it'll work. From any other class, it won't know what you're talking about, and might even insult you.
In (b), you're specifying a class in general - therefore it'll know what class to find it in - and it'll work if it's a "static method". *[see bottom]
In (c), you're specifying an instance of AClass you want to run "testing()" on*.
For instance, imagine you've created a class called Business. You make a hundred Business objects by specifying for each a name, number, address.
e.g.
Business b = new Business(name, number, address);
Then in the Business class you have a method "getName()". This method takes no argument - you could see that the brackets are empty - so if, from another class, you call "Business.getName()", how could it know which name you want? You've just made a hundred businesses!
It simply can't. Therefore, for such a method, you'd call "b.getName()" (b being the Business we created above) and it would get the name for this instance of a Business - namely, b.
I'm happy to help, so if you're confused about any particular parts of what I just wrote please let me know and I'll try to elaborate!
edit: A bit on static methods:
Static methods don't belong to an instance of the class. getName(), for example, would get the name of this Business - ie, this instance of the Business class. But let's say that in the Business class you made a method that took the first letter of each word in a String and transformed it to uppercase - like if you wanted to make the business names look more professional when you printed them out.
public static String stringToUpperCase(String aString){
aString = aString.substring(0, 1).toUpperCase() + aString.substring(1);
return aString;
}
And to use that, you change the getName() method from:
public String getName(){
return name;
}
to
public String getName(){
return stringToUpperCase(name);
}
The new method is used here to make the name have an uppercase first letter - but that is the extent of its involvement with the Business class. You notice it doesn't ask for information about the name, address, or number for a particular business. It just takes a string you give it, does something to it, and gives it back. It doesn't matter whether you have no Businesses or a hundred.
To call this method, you'd use:
System.out.println(Business.stringToUpperCase("hello"));
This would print Hello.
If it were not a static method, you'd have to make a new Business first:
Business b = new Business("aName", "aNumber", "anAddress");
System.out.println(b.stringToUpperCase("hello"));
And if the method did need access to more Business-instance information (like a business's name number or address) it wouldn't be able to be an instance variable.
The first example, a method without a return type at all, is a constructor; used when an instance is created with new. However, you can't return a value from a constructor. Something like,
this.amount = initial * interest; // return amount;
Sets the field amount to initial * interest.

'this' keyword in Java

I have a question regarding this in the following code. In the following, this.name will set the name. We could also do this using name = name, so my question is should the this pointer be used. This is not an homework
import java.io.*;
public class Employee{
String name;
int age;
String designation;
double salary;
//This is the constructor of the class Employee
public Employee(final String name){ //EDIT: Made parameter final
this.name = name;
//name= name; this is also correct
}
//Assign the age of the Employee to the variable age.
public void empAge(int empAge){
age = empAge;
}
//Assign the designation to the variable designation.
public void empDesignation(String empDesig){
designation = empDesig;
}
//Assign the salary to the variable salary.
public void empSalary(double empSalary){
salary = empSalary;
}
//Print the Employee details
public void printEmployee(){
System.out.println("Name:"+ name );
System.out.println("Age:" + age );
System.out.println("Designation:" + designation );
System.out.println("Salary:" + salary);
}
}
// name= name; this is also correct
This is not correct. It'll assign your parameter to itself. By using the this keyword, you're declaring which name you're using (i.e. the field on your Employee object).
You may wish to name the field differently from the parameter. However this means that all works well until someone automatically refactors your code to (perhaps inadvertently) declare the field and parameter as the same name!
For this reason you'll often see method signatures defined thus:
public Employee(final String name)
The final keyword prevents reassignment, and stops you from mistakenly reassigning the input parameter, and consequently not assigning to your field. Note also that if you declare the field as final, then compilation will fail if you don't make an assignment to that field. Using final is a good way to trap such errors and also enforce the immutability of an object (often a good thing - it contributes to a more reliable solution, especially in a threaded environment).
Well to avoid the confusion in cases like u mentioned name=name we uses this pointer to make it clear the we here mean class variable name .
So to make understand the reader here in this case we use this though there can be many other cases where (this) is more useful.
In some compilers name=name gives error as well
Notice that there are two things called name in your code. Your class Employee has member variable called name, and the constructor takes a parameter that's also called name.
What you want to do in the constructor is set the member variable name to the same value as the parameter name. To access the member variable, you have to use this.name, because name refers to the parameter - because the variables have the same name, the parameter is hiding the member variable.
Note that name = name; does not do the same thing as this.name = name;.
When you do name = name;, you assign the value of the parameter name to the parameter itself - not to the member variable. The compiler does not magically know that the first name is supposed to mean the member variable, and the second name is supposed to mean the parameter.
So, you need this in this case to refer explicitly to the member variable, instead of the parameter that is hiding the member variable.
In this case this is just a scope resolution/disambiguation.
public Employee(String name){
this.name = name;
// the above line will assign a value of parameter to instance variable
// name= name; this is also correct
// (**NO** the above line will assign a value of parameter to itself)
}
When the parameter name and your class variable names are same, then to differentiate between them, you write this.classVariable to identify the class variale
When you call a variable, the pointing one is the one who is the closest to your current scope.
So, if program is currently containing in memory a local variable toto and the wrapping class containing a field variable toto, you have to precise this keyword in order to access the field's one.
Otherwise, the field variable is said to be shadowed by the local variable and so doing toto = toto assigns the local parameter to itself (never useful) and not what you are expecting => the field variable.
Why make it so hard on yourself?
Just rewrite the signature/method like this:
public Employee(String _name) {
name = _name;
}
Always try to avoid variable hiding or other hard-to-read constructs. If you want your code to be maintainable, write it in such a way that everybody can understand it immediately. Even if you are the only one; you might forget what you meant in time.
This Keyword Program
public class ThisKeywordActivity extends Activity {
int a=20;
public ThisKeywordActivity(int a) { // this();
//this.a = a;
}
public ThisKeywordActivity()
{
System.out.println(" msg default Constructor ");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_this_keyword);
ThisKeywordActivity thisKeywordActivity2 = new ThisKeywordActivity(100);
thisKeywordActivity2.show(40);
}
public void show( int a)
{
System.out.println("msg a 2==="+a);
System.out.println("msg a 3==="+this.a);
}
}
Output
a2=40
a3=20

Pass by "Reference Value"? Some clarification needed

I know that in Java, everything is passed by value. But for objects, it is the value of the reference to the object that is passed. This means that sometimes an object can get changed through a parameter, which is why, I guess, people say, Never modify parameters.
But in the following code, something different happens. s in changeIt() doesn't change when you get back to main():
public class TestClass {
static String str = "Hello World";
public static void changeIt( String s ) {
s = "Good bye world";
}
public static void main( String[] args ) {
changeIt( str );
System.out.println( str );
}
}
I'm guessing -- and I'd like confirmation -- that when you say s = "something" it's the same or equivalent to saying String s = new String("something"). Is this why s doesn't change? Is it assigned a whole new object locally which gets thrown away once you exit changeIt()?
that when you say s = "something" it's the same or equivalent to saying String s = new String("something")
Yes, pretty much. (though the JVM might do optimizations so that the same string literal used several times refers to the same String object).
Is this why s doesn't change? Is it assigned a whole new object locally which gets thrown away once you exit changeIt()
Yes. As you say, everything is passed by value in Java, even references to object. So the variable s in changeIt( String s ) is a different value from str you use in main(), it's just a local variable within the changeIt method.
Setting that reference to reference another object does not affect the caller of changeIt.
Note that the String object s refer to is still the same String as str refers to when entering the changeIt() method before you assign a different object to s
There's another thing you need to be aware of, and that is that Strings are immutable. That means that no method you invoke on a string object will change that string. e.g. calling s.toLowerCase() within your changeIt() method will not affect the caller either. That's because the String.toLowerCase() does not alter the object, but rather returns a new String object.
When you write
s = "Good bye world";
you are changing the value of s to be a reference to the new string. You are not changing the value of the string referenced by s.
Yes, now 'S' points to brand new object whose scope is limited to that method. String may not be perfect example to understand pass-by-value concept. Instead of string, let us say pass some mutable object reference and make changes to that assign new object inside the method. You don't see them outside of the object.
public class MyMain {
private static void testMyMethod(MyMain mtest) {
mtest=new MyMain();
mtest.x=50;
System.out.println("Intest method"+mtest.x);
}
int x=10;
public static void main(String... args)
{
MyMain mtest = new MyMain();
testMyMethod(mtest);
System.out.println("In main method: "+mtest.x);
}
}
Read second answers in this SO discussion.

When creating methods and passing objects as parameters, are they copied or referenced? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is Java pass by reference?
see example below... need java.io library to run...
public class BlankClass extends ConsoleProgram {
public void run() {
while(true) {
setFont("London-24");
String name = readLine("Type a name: ");
fixName(name);
/* I know that the way this is written doesn't make sense and that println(fixName(name))
* is the right way. However, I thought that when objects then the method is using the object
* (in this case a string) and not a copy of it. In other words, it is referenced.
* So if it is referenced why isn't it printing out Steven when I give it STEVEN.
*/
//println(fixName(name); this is removed to show the question.
println(name);
}
}
private String fixName(String name) {
char first = name.charAt(0);
first = Character.toUpperCase(first);
name = name.substring(1);
name = first + name.toLowerCase();
return name;
}
}
Java always passes parameters by value - but in the case of classes/objects, the value that's passed is a reference, not an object itself.
What the type involved, the value of the argument expression is copied as the initial value of the parameter. Changes to the parameter variable itself are not seen by the caller, whereas changes to the object that the reference refers to will be seen.
For example, using StringBuilder (which is a mutable type):
public void foo(StringBuilder builder)
{
builder = new StringBuilder("Change to builder");
}
public void bar(StringBuilder builder)
{
builder.append(" - appended");
}
Now:
StringBuilder x = new StringBuilder("Original value");
foo(x);
System.out.println(x); // Still prints "Original value"
StringBuilder y = new StringBuilder("Original value 2");
bar(y);
System.out.println(y); // Prints "Original value 2 - appended"
Note that when I say "the value of the argument expression", that is never an object - it's either a primitive value, or a reference.
I like to think of an analogy with houses. Suppose you have a piece of paper (a variable) with directions to a house written on it. You call a method and use that variable as the argument - that creates a new piece of paper (the parameter) with the same directions on. If the method crosses out the original directions and replaces them with some other ones, that doesn't change the first piece of paper. On the other hand, if the method follows the directions and then paints the house red, then you would see that change if you followed the directions on the first piece of paper.
EDIT: To explain your original code... no objects are being copied, but the value of name in run is being copied into fixName. You're then changing the value of the parameter in fixName when you write this:
name = name.substring(1);
You're changing it again when you write:
name = first + name.toLowerCase();
Neither of these have changed the value of name in the calling code, which is still referring to the original string.
You're then returning the new string reference here:
return name;
but your calling code is completely ignoring it, because you've just written:
fixName(name);
One way to demonstrate what's happened is to use the return value in a new variable:
String fixedName = fixName(name);
Then you could print out name (which would show the original string) and fixedName (which would show the new one).
you pass a reference, so you work with the same string, BUT you return another string, because String in java is immutable - every operation (such as subString) produce new string and if you want to perform many operations on string (such as substring, replace etc.) use a StringBuffer or StringBuilder
This does not really answer your question, but you should avoid assigning parameters (like 'name' in this case), it can be handy at times but it is generally considered a bad practice because it often leads to unreadable and hard to maintain code.
In your case the variable is both a parameter and a local variable.
In Eclipse there is a warning you can activate for this in
Preferences->Java->Compiler->Errors/Warnings->Code style->Parameter assignment
I would recommend to set the parameter 'name' final in order to enforce this.
Return another String that is based on your 'name' String and name it properly.
The goal is that anyone reading your code should be able to quickly understand what is going on by elimination (the function is private, it is static, the parameter is final...). This excludes a lot of side effects.
Search for the concept of 'pure functions' on the web. Make the method static so the person reading your code knows that there are no side effects on the instance.
Here is the new version:
private static String fixName(final String name) {
final char firstCharOfName = Character.toUpperCase(name.charAt(0));
final String fixedName = firstCharOfName + name.substring(1).toLowerCase();
return fixedName;
}

Categories