How do I know which variables exist in an object? - java

I was given this code:
//Class X is created, and then class Y is derived from class X:
class X
{
protected int m;
}
class Y extends X
{
private int n;
public Y (int m, int n)
{
this.m = m;
this.n = n;
}
public String toString ()
{
return m + ", " + n;
}
}
//Class Y is used in the following way:
class UseY
{
public static void main (String[] args)
{
Y y = new Y (3, 4);
System.out.println (y);
}
}
So as you can see, the code involves inheritance.
I was asked:
Which output is created when the program UseY is executed? Which variables exist in object y?
My answer:
The output is:
3,4.
The variables in object y are m and n.
But I'm not sure about my second answer. What do they mean by variables exactly? Is my answer correct?

I would say:
The object y has a nested member, called n and a derived member from its super-class, called m.

The object y of type Y contains two variables; an int "m" and an int "n"
Output: 3, 4

Your answer to the first question is correct.
What do they mean by variables exactly?
This tutorial page explains the exact meaning of variables.
The Java programming language defines the following kinds of variables:
Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.
Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
Parameters You've already seen examples of parameters, both in the Bicycle class and in the main method of the "Hello World!" application. Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.
In this case, the variables present are the instance fields n and m (type 1 above). The object of type Y has n already defined in its class. Since it extends X, it inherits the class member field m as well. A subclass inherits all of the public and protected members of its parent. Therefore, the variables present in the object of type Y are members m and n.

Related

Java output explanation [duplicate]

Consider the int a variables in these classes:
class Foo {
public int a = 3;
public void addFive() { a += 5; System.out.print("f "); }
}
class Bar extends Foo {
public int a = 8;
public void addFive() { this.a += 5; System.out.print("b " ); }
}
public class test {
public static void main(String [] args){
Foo f = new Bar();
f.addFive();
System.out.println(f.a);
}
}
I understand that the method addFive() have been overridden in the child class, and in class test when the base class reference referring to child class is used to call the overridden method, the child class version of addFive is called.
But what about the public instance variable a? What happens when both base class and derived class have the same variable?
The output of the above program is
b 3
How does this happen?
There are actually two distinct public instance variables called a.
A Foo object has a Foo.a variable.
A Bar object has both Foo.a and Bar.a variables.
When you run this:
Foo f = new Bar();
f.addFive();
System.out.println(f.a);
the addFive method is updating the Bar.a variable, and then reading the Foo.a variable. To read the Bar.a variable, you would need to do this:
System.out.println(((Bar) f).a);
The technical term for what is happening here is "hiding". Refer to the JLS section 8.3, and section 8.3.3.2 for an example.
Note that hiding also applies to static methods with the same signature.
However instance methods with the same signature are "overridden" not "hidden", and you cannot access the version of a method that is overridden from the outside. (Within the class that overrides a method, the overridden method can be called using super. However, that's the only situation where this is allowed. The reason that accessing overridden methods is generally forbidden is that it would break data abstraction.)
The recommended way to avoid the confusion of (accidental) hiding is to declare your instance variables as private and access them via getter and setter methods. There are lots of other good reasons for using getters and setters too.
It should also be noted that: 1) Exposing public variables (like a) is generally a bad idea, because it leads to weak abstraction, unwanted coupling, and other problems. 2) Intentionally declaring a 2nd public a variable in the child class is a truly awful idea.
From JLS
8.3.3.2 Example: Hiding of Instance Variables This example is similar to
that in the previous section, but uses
instance variables rather than static
variables. The code:
class Point {
int x = 2;
}
class Test extends Point {
double x = 4.7;
void printBoth() {
System.out.println(x + " " + super.x);
}
public static void main(String[] args) {
Test sample = new Test();
sample.printBoth();
System.out.println(sample.x + " " +
((Point)sample).x);
}
}
produces the output:
4.7 2
4.7 2
because the declaration of x in class
Test hides the definition of x in
class Point, so class Test does not
inherit the field x from its
superclass Point. It must be noted,
however, that while the field x of
class Point is not inherited by class
Test, it is nevertheless implemented
by instances of class Test. In other
words, every instance of class Test
contains two fields, one of type int
and one of type double. Both fields
bear the name x, but within the
declaration of class Test, the simple
name x always refers to the field
declared within class Test. Code in
instance methods of class Test may
refer to the instance variable x of
class Point as super.x.
Code that uses a field access
expression to access field x will
access the field named x in the class
indicated by the type of reference
expression. Thus, the expression
sample.x accesses a double value, the
instance variable declared in class
Test, because the type of the variable
sample is Test, but the expression
((Point)sample).x accesses an int
value, the instance variable declared
in class Point, because of the cast to
type Point.
In inheritance, a Base class object can refer to an instance of Derived class.
So this is how Foo f = new Bar(); works okay.
Now when f.addFive(); statement gets invoked it actually calls the 'addFive() method of the Derived class instance using the reference variable of the Base class. So ultimately the method of 'Bar' class gets invoked. But as you see the addFive() method of 'Bar' class just prints 'b ' and not the value of 'a'.
The next statement i.e. System.out.println(f.a) is the one that actually prints the value of a which ultimately gets appended to the previous output and so you see the final output as 'b 3'. Here the value of a used is that of 'Foo' class.
Hope this trick execution & coding is clear and you understood how you got the output as 'b 3'.
Here F is of type Foo and f variable is holding Bar object but java runtime gets the f.a from the class Foo.This is because in Java variable names are resolved using the reference type and not the object which it is referring.

What happens to variables/objects in inner classes of function objects?

I have a function multi2 which returns inner class Inner as an Object.
What happens to a - where is it saved and how can I access it?
public class C {
private static Object multi2(final int a) {
class Inner {
public int hashCode() {
return 2*a;
}
}
return new Inner(); // What happens to a?
// Who allocates a?
// Can I Access a?
}
public static void main(String[] args) {
Object o = multi2(6);
System.out.println("o.hashCode() = " + o.hashCode());
o = multi2(4);
System.out.println("o.hashCode() = " + o.hashCode());
}
}
What happens at the implementation level is that a copy of the value of a is saved in a synthetic instance variable declared in the compiled version of the C.Inner class.
The value of a is passed to the compiled Inner constructor via an extra parameter.
The C.Inner.hashCode method uses the value of the synthetic variable. Accessing a in the source code of Inner.hashCode is transformed into accessing the corresponding synthetic variable in the compiled code.
The variable in the outer scope must be final1. The synthetic variable must be final2 in the Inner class. This maintains the illusion that (potentially) multiple instances of the Inner class are seeing the same a variable. (They aren't, but since the variable(s) can't be changed, it is not possible for the code of the inner class to tell the difference.)
If you use javap to look at the bytecodes for the compiled example, you will see the mechanisms used to implement this in the outer and the inner classes.
1 - or effectively final from Java 8 onwards.
2 - If a could be mutated by an Inner method, then two Inner instances with the same outer class need to share a mutable variable whose lifetime is (now) longer than the stackframe for a multi2 call. That entails somehow turning a from stack variable into something that lives on the heap. It would be expensive and complicated.
You have defined the class Inner inside the function so the scope of the class will be
restricted with in the method. And your function is static so it will be live as long as the class definition is loaded. You have override the hashCode function inside the InnerClass so every time you are calling the multi2(param) you are creating the hashCode for the instance of InnerClass and returning the instance of the InnerClass.
So as for you questions, please correct me if i am wrong.
What happens to a ?
a is with in the scope of your static method, so it will be live as long as the class definition is loaded.
Who allocates a?
scope of a is restricted inside the static method and static method does not require instance to access it but as for the static method/variable allocation, i think it depends on JVM.
Can I Access a?
No you cannot access a from outside you static method, it is restricted with in your static method.
Since the "a" is a local parameter, you could use a different approach to read the "a" value:
public class C {
public static Object multi2(final int a) {
return new Inner(a);
}
public static void main(String[] args) {
Object o = multi2(6);
System.out.println("o.hashCode() = " + o.hashCode());
System.out.println("o.getA() = " + ((Inner) o).getA());
o = multi2(4);
System.out.println("o.hashCode() = " + o.hashCode());
System.out.println("o.getA() = " + ((Inner) o).getA());
}
}
class Inner{
public int valueA;
public Inner(int a)
{
valueA = a;
}
public int getA() {
return valueA;
}
public int hashCode() {
return 2*valueA;
}
}
I wanted to know what was actually happening, so I compiled your code and looked at the bytecode output.
Basically what happens is the compiler adds in a constructor to your class 'Inner'. It also adds a single parameter to that constructor which takes 'a'. If your multi2() method was NOT static then there would probably also be a parameter to take 'this' where 'this' is the instance of 'C' that multi2() is executing on. BUT since we're in static context, there is no 'this'.
The compiler adds a private final field to your class 'Inner' and sets that private field using the value passed via the constructor. The compiler also converts
new Inner()
into
new Inner(a)
Hashcode then accesses the private field containing the value for a.
If 'a' was an object instead of a primitive, then it would be the same way, but a reference would be passed through instead of an actual number value.
How do you access this variable? Well you access it with reflections, but there are many problems:
1) You don't know the name of the field made by the compiler, so you can only get the name by looking at the bytecode. Don't trust decompilers as they might change the name. You gotta look at the bytecode yourself to find out.
2) The compiler probably marks the field as final, which means even if you can get reflections to access the field for you, you won't be able to update it.
3) It is entirely up to the compiler to figure out field names. Field names could change between builds depending on the compiler and it's mood.
Inner is a so called local class. a is a parameter passed to the method multi2 and accessable within that scope. Outside of that method, you cannot access a.

Java static variables and local variables [duplicate]

This question already has answers here:
Why is it not possible to shadow a local variable in a loop?
(7 answers)
Closed 5 years ago.
I am new in Java and came across one OCJA-1.8 sample question where I am having some doubt. I need clarification on this behavior of JVM.
public class Test{
static int x=1;//**This is static class level variable**
public static void main(String[] args){
int[] nums={1,2,3,4,5};
for(int x:nums){ // Local variable declared for for-each loop.
System.out.println(x);
}
}
}
Why the JVM have not thrown the error as duplicate variable, For the variable which is declared inside for each loop 'int x'. As static variable has class level scope ?
The local variable hides the original static field somewhat, but it is not inaccessible:
Test.x
And for non-static fields:
this.x // not in this case
So it is allowed, and in effect one often sees:
public class Pt {
private final int x;
public Pt(int x) {
this.x = x;
}
}
Which prevents the need to introduce some convention (_x, mX).
What is not allowed:
void f() {
int x = 42;
if (true) {
int x = 13; // COMPILER ERROR
...
}
}
As this is bad style, causing confusion.
The inner variable declaration of x override the static one.
If you need to access the static variable you can access it with Test.x
The duplicate variable compilation error happens for two variables with the same name declared in the same scope : fields or method declaration scope.
In your example, each variable is declared in a distinct scope.
As a consequence, as you refer x in the method declaring x, by default it refers to the variable with the nearer scope available (the x local variable) that so shadows the other variable (the x field variable).
To reference the shadowed variable :
if it is a static field (your case), prefix it with the class name declaring it : Test.x
if it is an instance field, prefix it with the this keyword : this.x
When you have a local variable same as static variable, static variable of the class is shadowed by the local variable.
It doesn't throw an error because Java has the concept of shadowing. In essence, the variable with the lowest scope is used.
The static field is still accessible, you just need to fully qualify it:
for(int x:nums){
System.out.println(x); // Local x
System.out.println(Test.x); // static x
}
This can be confusing to a reader of your code and so should be avoided in most cases. If you find yourself with a field and local variable named identically, there's probably something up and you should re-evaluate how you've named your variables.
In your specific case, x is not a descriptive name and both variables would benefit from better, more descriptive names.
In certain cases, such as within a constructor or setter method, it's beneficial to have local variables and fields with the same names, and this is where shadowing is a useful feature.

Difference between Static and final?

I'm always confused between static and final keywords in java.
How are they different ?
The static keyword can be used in 4 scenarios
static variables
static methods
static blocks of code
static nested class
Let's look at static variables and static methods first.
Static variable
It is a variable which belongs to the class and not to object (instance).
Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.
A single copy to be shared by all instances of the class.
A static variable can be accessed directly by the class name and doesn’t need any object.
Syntax: Class.variable
Static method
It is a method which belongs to the class and not to the object (instance).
A static method can access only static data. It can not access non-static data (instance variables) unless it has/creates an instance of the class.
A static method can call only other static methods and can not call a non-static method from it unless it has/creates an instance of the class.
A static method can be accessed directly by the class name and doesn’t need any object.
Syntax: Class.methodName()
A static method cannot refer to this or super keywords in anyway.
Static class
Java also has "static nested classes". A static nested class is just one which doesn't implicitly have a reference to an instance of the outer class.
Static nested classes can have instance methods and static methods.
There's no such thing as a top-level static class in Java.
Side note:
main method is static since it must be be accessible for an application to run before any instantiation takes place.
final keyword is used in several different contexts to define an entity which cannot later be changed.
A final class cannot be subclassed. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example java.lang.System and java.lang.String. All methods in a final class are implicitly final.
A final method can't be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.
A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a blank final variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases.
Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.
When an anonymous inner class is defined within the body of a method, all variables declared final in the scope of that method are accessible from within the inner class. Once it has been assigned, the value of the final variable cannot change.
static means it belongs to the class not an instance, this means that there is only one copy of that variable/method shared between all instances of a particular Class.
public class MyClass {
public static int myVariable = 0;
}
//Now in some other code creating two instances of MyClass
//and altering the variable will affect all instances
MyClass instance1 = new MyClass();
MyClass instance2 = new MyClass();
MyClass.myVariable = 5; //This change is reflected in both instances
final is entirely unrelated, it is a way of defining a once only initialization. You can either initialize when defining the variable or within the constructor, nowhere else.
note A note on final methods and final classes, this is a way of explicitly stating that the method or class can not be overridden / extended respectively.
Extra Reading
So on the topic of static, we were talking about the other uses it may have, it is sometimes used in static blocks. When using static variables it is sometimes necessary to set these variables up before using the class, but unfortunately you do not get a constructor. This is where the static keyword comes in.
public class MyClass {
public static List<String> cars = new ArrayList<String>();
static {
cars.add("Ferrari");
cars.add("Scoda");
}
}
public class TestClass {
public static void main(String args[]) {
System.out.println(MyClass.cars.get(0)); //This will print Ferrari
}
}
You must not get this confused with instance initializer blocks which are called before the constructor per instance.
The two really aren't similar. static fields are fields that do not belong to any particular instance of a class.
class C {
public static int n = 42;
}
Here, the static field n isn't associated with any particular instance of C but with the entire class in general (which is why C.n can be used to access it). Can you still use an instance of C to access n? Yes - but it isn't considered particularly good practice.
final on the other hand indicates that a particular variable cannot change after it is initialized.
class C {
public final int n = 42;
}
Here, n cannot be re-assigned because it is final. One other difference is that any variable can be declared final, while not every variable can be declared static.
Also, classes can be declared final which indicates that they cannot be extended:
final class C {}
class B extends C {} // error!
Similarly, methods can be declared final to indicate that they cannot be overriden by an extending class:
class C {
public final void foo() {}
}
class B extends C {
public void foo() {} // error!
}
static means there is only one copy of the variable in memory shared by all instances of the class.
The final keyword just means the value can't be changed. Without final, any object can change the value of the variable.
final -
1)When we apply "final" keyword to a variable,the value of that variable remains constant.
(or)
Once we declare a variable as final.the value of that variable cannot be changed.
2)It is useful when a variable value does not change during the life time of a program
static -
1)when we apply "static" keyword to a variable ,it means it belongs to class.
2)When we apply "static" keyword to a method,it means the method can be accessed without creating any instance of the class
Think of an object like a Speaker. If Speaker is a class, It will have different variables such as volume, treble, bass, color etc. You define all these fields while defining the Speaker class. For example, you declared the color field with a static modifier, that means you're telling the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.
Declaring
static final String color = "Black";
will make sure that whenever this class is instantiated, the value of color field will be "Black" unless it is not changed.
public class Speaker {
static String color = "Black";
}
public class Sample {
public static void main(String args[]) {
System.out.println(Speaker.color); //will provide output as "Black"
Speaker.color = "white";
System.out.println(Speaker.color); //will provide output as "White"
}}
Note : Now once you change the color of the speaker as final this code wont execute, because final keyword makes sure that the value of the field never changes.
public class Speaker {
static final String color = "Black";
}
public class Sample {
public static void main(String args[]) {
System.out.println(Speaker.color); //should provide output as "Black"
Speaker.color = "white"; //Error because the value of color is fixed.
System.out.println(Speaker.color); //Code won't execute.
}}
You may copy/paste this code directly into your emulator and try.
Easy Difference,
Final : means that the Value of the variable is Final and it will not change anywhere. If you say that final x = 5 it means x can not be changed its value is final for everyone.
Static : means that it has only one object. lets suppose you have x = 5, in memory there is x = 5 and its present inside a class. if you create an object or instance of the class which means there a specific box that represents that class and its variables and methods. and if you create an other object or instance of that class it means there are two boxes of that same class which has different x inside them in the memory. and if you call both x in different positions and change their value then their value will be different. box 1 has x which has x =5 and box 2 has x = 6. but if you make the x static it means it can not be created again.
you can create object of class but that object will not have different x in them.
if x is static then box 1 and box 2 both will have the same x which has the value of 5. Yes i can change the value of static any where as its not final. so if i say box 1 has x and i change its value to x =5 and after that i make another box which is box2 and i change the value of box2 x to x=6. then as X is static both boxes has the same x. and both boxes will give the value of box as 6 because box2 overwrites the value of 5 to 6.
Both final and static are totally different. Final which is final can not be changed. static which will remain as one but can be changed.
"This is an example. remember static variable are always called by their class name. because they are only one for all of the objects of that class. so
Class A has x =5, i can call and change it by A.x=6; "
Static and final have some big differences:
Static variables or classes will always be available from (pretty much) anywhere. Final is just a keyword that means a variable cannot be changed. So if had:
public class Test{
public final int first = 10;
public static int second = 20;
public Test(){
second = second + 1
first = first + 1;
}
}
The program would run until it tried to change the "first" integer, which would cause an error. Outside of this class, you would only have access to the "first" variable if you had instantiated the class. This is in contrast to "second", which is available all the time.
Static is something that any object in a class can call, that inherently belongs to an object type.
A variable can be final for an entire class, and that simply means it cannot be changed anymore. It can only be set once, and trying to set it again will result in an error being thrown. It is useful for a number of reasons, perhaps you want to declare a constant, that can't be changed.
Some example code:
class someClass
{
public static int count=0;
public final String mName;
someClass(String name)
{
mname=name;
count=count+1;
}
public static void main(String args[])
{
someClass obj1=new someClass("obj1");
System.out.println("count="+count+" name="+obj1.mName);
someClass obj2=new someClass("obj2");
System.out.println("count="+count+" name="+obj2.mName);
}
}
Wikipedia contains the complete list of java keywords.
I won't try to give a complete answer here. My recommendation would be to focus on understanding what each one of them does and then it should be cleare to see that their effects are completely different and why sometimes they are used together.
static is for members of a class (attributes and methods) and it has to be understood in contrast to instance (non static) members. I'd recommend reading "Understanding Instance and Class Members" in The Java Tutorials. I can also be used in static blocks but I would not worry about it for a start.
final has different meanings according if its applied to variables, methods, classes or some other cases. Here I like Wikipedia explanations better.
Static variable values can get changed although one copy of the variable traverse through the application, whereas Final Variable values can be initialized once and cannot be changed throughout the application.

What is the "non-static method" error and how does "this" work?

I have a couple of EXTREMELY basic Java questions that I would like to finally understand, once and for all. I have the following short piece of code:
public class VeryBasicJava{
public static void main(String[] args){
int x = 3;
int y = 4;
swapMe(x, y);
}
private void swapMe(int a, int b){
int a;
int b;
int tmp = a;
this.a = b;
this.b = a;
}
}
When I compile, I get the dreaded "non-static method swapMe(int,int) cannot be referenced from a static context" error. Also, I get "a is already defined in swapMe(int,int)" and "b is already defined in swapMe(int,int)"
What I need to finally get through my thick skull, is the "non-static method" error, how (why) it is caused, and how to avoid it.
Further, I was under the assumption that you could do what I am attempting to do with my 'a' and 'b' variables in the "swapMe" method. I thought I could pass in an 'a' and 'b', but also create new variables 'a' and 'b', and reference them with "this" keyword.
I know this is extremely basic, but these two "issues" are two of the main sticking points I have with Java, and for some reason, cannot seem to properly learn.
Thank you all, for taking the time to read this. Have a great day.
That means that the method swapMe() is an instance method and you need an instance of the VeryBasicJava class to invoke it, like this:
VeryBasicJava instance = new VeryBasicJava();
int x = 3; int y = 4;
instance.swapMe(x, y);
Alternatively, you could declare swapMe() as static, in that way you won't need to create an instance first:
private static void swapMe(int a, int b)
You have just a few minor problems. You mentioned in a comment that you have some experience with C, so I’ll try to draw some basic analogies. A static method (such as main) behaves like an ordinary C function. A non-static method, however, takes a hidden parameter: this, which refers to an object on which that method is to operate. When you write a method such as this:
private void swapMe(int a, int b) {
// ...
It really means something like this:
private void swapMe(VeryBasicJava this, int a, int b){
// ^^^^^^^^^^^^^^^^^^^^
// ...
Because the this parameter is treated specially, there is a special syntax for calling non-static methods on objects:
myInstance.swapMe(someA, someB);
// ^^^^^^^^^^ ^^^^^ ^^^^^
// this a b
And because swapMe is not declared static, it expects to be called like the above.
The fact that main is declared inside the VeryBasicJava class does not mean that you automatically have a VeryBasicJava object. Again, because main is static, it is just like an ordinary C function:
void VeryBasicJava_main(...) {
// ...
In order to create an instance of an object, you use new:
VeryBasicJava vbj = new VeryBasicJava();
This is analogous to malloc in C:
VeryBasicJava *vbj = malloc(sizeof(VeryBasicJava));
VeryBasicJava_construct(vbj);
With an instance, you can invoke a method:
vbj.swapMe(spam, eggs);
Your other issue is twofold: one of scope, and of members. Scope, as you may know, refers to where a variable exists. Take this function:
1: private void swapMe(int a, int b) {
2: int a;
3: int b;
4: int tmp = a;
5: this.a = b;
6: this.b = a;
7: }
When this method is called, the following things happen:
a and b are created, given the values specified in the call to swapMe.
A new a is created, local to swapMe, with the default value 0. This a hides the parameter a, and there is no way to differentiate them.
A new b is created, also strictly local. It too has the default 0 value, and hides the parameter b.
tmp is created, and its value is set to that of the newly declared a, so it too is 0.
[see below]
[see below]
The locals a and b cease to exist, as do the parameters a and b.
In lines 5 and 6, you attempt to use the syntax this.a to refer to the local a rather than the parameter. Though this syntax exists in Java, it does not do what you mean. Parameters are treated just the same as local variables, so rather than differentiating between those two categories, this.a differentiates between locals and members. Now, what are members? Well, say your class declaration contains variable declarations:
class VeryBasicJava {
private int a;
private int b;
// ...
}
That’s just like member declarations in a C struct:
struct VeryBasicJava {
int a;
int b;
};
What this means is that when you create an instance of VeryBasicJava:
VeryBasicJava vbj = new VeryBasicJava();
That instance has its own variables a and b, which can be used in any non-static method:
public void print() {
System.out.println("a is " + a);
System.out.println("b is " + b);
}
If you have a local variable with the same name as a member, then you must use this to explicitly state that you want to refer to the member. This complete program illustrates how to declare, use, and differentiate between members and locals.
class VeryBasicJava {
private int a;
private int b;
private int c;
public static void main(String[] args) {
VeryBasicJava vbj = new VeryBasicJava();
vbj.a = 3;
vbj.b = 4;
vbj.c = 5;
vbj.print(1);
}
public void print(int a) {
int b = 2;
System.out.println("a is " + a);
System.out.println("b is " + b);
System.out.println("c is " + c);
System.out.println("this.a is " + this.a);
System.out.println("this.b is " + this.b);
System.out.println("this.c is " + this.c);
}
}
It will produce this output:
a is 1
b is 2
c is 5
this.a is 3
this.b is 4
this.c is 5
I hope these examples and explanations are helpful.
Your problems stem from not understanding how the object-orientated paradigm works.
The idea is that you define a class, which is a "type of thing". From that class, you create instances, which are effectively examples of that type of thing.
For example, when you create a class "Wallet", that defines how your
system will treat wallets (i.e. what they can do, and what you can do
with them). You can now create multiple different instances of
wallets, that store different values, but still work the same way.
(e.g. "myWallet" and "yourWallet" are both Wallets, so you can take
money out and put money in etc, but if I check the money in
yourWallet, I get a value of 50, whereas if I check the value in
myWallet, I get a value of 20)
Now, "static" methods belong to the class. This means that they can't access members variables specific to the class. For example, if I'm keeping track of every wallet in my system, that value doesn't belong to any particular wallet in the class, but it's still involved with that class, so we make it static.
To use my example, if you have two objects of type "Wallet", and one
has its "cash" value set to 30, and the other has its cash value set
to "25", a static function can't access both values, so it can't
access either. You have to give it a particular wallet to access.
The advantage of this method over C's approach is that it ties related functionality together. For example, if you see a C array of ints, you don't really know what they are intended to represent and may do something unexpected with them. But if I give you a Java array of Wallets, you also get the methods that influence how you interact with them. (This is a really stripped down explanation of encapsulation)
One of the principle issues with Java is that it ties everything to a class. This causes a problem for the main method, which has to start the program, because it has no instances yet, but must still be part of the class. Therefore the main method must always be static. This tends to confuse new programmers, as invariably the first thing you do within the main method is create an instance of the class it's "inside".
In your example, you've got no "specific" data. Everything's inside the methods, which means you're effectively trying to write procedural code in Java. You can do this (just declare everything static), but the language will fight you, and you'll write very bad code.
When I was originally starting out with Java, I effectively treated the main() method of a class as if it wasn't part of the class. Conceptually, it helped, because you stop thinking of the non-static stuff as something that's already there (which is your main issue). But my recommendation to you is try reading through this. But there are some very good transition courses out there for learning object-orientated programming, and you may want to invest in them.
Classes are a template for OBJECTS. An instance of a class (i.e. and object) has its own version of variables and methods that are non-static.
static methods and fields are not tied to any specific instance of a class.
So it makes no sense to invoke a class instance method from a static method, without the instance on which you want to invoke the method. In your example, this is a reference to an instance, but you have no instance....
For your second question, when you create a function, it creates its own little stack for all of its variables. Its arguments are included in that stack - so, your stack looks like this:
int a; //this is the first argument
int b; //this is the second argument
int a; //this is the first line of your function
int b; //this is the second line of your function
That's why you're getting an issue - the a and b you declare after your arguments are conflicting with your arguments, not "this.a" and "this.b".
Your first question has already been answered.
As the fix's been provided, I would like to add:
static methods can only call other static methods and access only static variables directly (in java).
To call non-static methods, you have create an instance of that class and call the methods then.
swapMe needs an instance of VeryBasicJava to invoke it. Static does not.
From outside that object, you could call: VeryBasicJava.main(args).
From outside the object, you would need to do (new VeryBasicJava()).swapMe(a,b) to invoke that method.
The reason you are getting errors inside of swapMe is because you are defining those variables twice. The signature line declared them in that namespace for you. In that method, you already have an int a. Then on the first line, you declare int a again.
What you want to do in your swap is something like this:
private void swapMe(int a, int b){
int tmp = a;
a = b;
b = tmp;
}
Of course the variables a and b only exist in that method so as soon as the method ends your work is lost. If you wanted to keep it, make the variables class variables and provide getters and setters for them.

Categories

Resources