import comp102x.IO;
public class testing {
private int x;
public testing(int x) {
x = x;
}
public static void main(String[] args) {
testing q1 = new testing(10);
IO.outputln(q1.x);
}
}
Why is the output 0 instead of 10? This is a JAVA script. The concept of instance and local variables is very confusing to me, can someone help to explain?
public testing(int x) {
x = x;
}
here you only reassign the value of the local variable x to itself, it doesn't change the instance variable.
Either change the name of the local variable, like this:
public testing(int input) {
x = input;
}
or make sure you assign the value to the instance variable, like this:
public testing(int x) {
this.x = x;
}
Related
#include<stdio.h>
void decrease(int *i);
int main(){
int i = 10;
decrease(&i);
printf("%d",i);
}
void decrease(int *i){
*i = *i - 1;
}
What would be the Java program for the same?
As you pointed out (no pun intended), Java does not support pointers. So, there is no way to directly manipulate the value of a primitive passed to a method, because only a copy of the primitive would be used in the method. One way to get around this would be to just return the updated value, and then overwrite the integer in the calling scope:
public static int decrease(int i) {
return i - 1;
}
public static void main(String[] args) {
int i = 10;
i = decrease(i);
System.out.println(i); // prints 9
}
You have two options, either (return) the value, and modify it in the main class, or pass an Object, not a primitive.
An object with your values:
public class Holder {
public int x;
}
And a method to modify it
public void modify(Holder h){
h.x = 2;
}
Called like:
Holder h = new Holder();
h.x = 1;
modify(h);
System.out.println(h.x);
Results in:
2
How do i print the value of variable which is defined inside another method?
This might be a dumb question but please help me out as i am just a beginner in programming
public class XVariable {
int c = 10; //instance variable
void read() {
int b = 5;
//System.out.println(b);
}
public static void main(String[] args) {
XVariable d = new XVariable();
System.out.println(d.c);
System.out.println("How to print value of b here? ");
//d.read();
}
}
You can't. b is a local variable. It only exists while read is executing, and if read executes multiple times (e.g. in multiple threads, or via recursive calls) each execution of read has its own separate variable.
You might want to consider returning the value from the method, or potentially using a field instead - it depends on what your real-world use case is.
The Java tutorial section on variables has more information on the various kinds of variables.
You need to return value from your read() methods.
public class XVariable {
int c = 10; //instance variable
int read() {
int b = 5;
return b;
}
public static void main(String[] args) {
XVariable d = new XVariable();
System.out.println(d.c);
System.out.println(read());
//d.read();
}
}
Return b from the read method and print it
public class XVariable {
int c = 10; //instance variable
int read() {
int b = 5;
return b;
}
public static void main(String[] args) {
XVariable d = new XVariable();
System.out.println(d.c);
System.out.println(d.read());
}
}
Here when I run this below code I get called as the output and I was wondering why not called new. Since 1 comes under both short and int range.
public class MyClass {
private int x;
public MyClass(){
this(1);
}
public MyClass(int x){
System.out.println("called");
this.x = x;
}
public MyClass(short y){
System.out.println("called new");
this.x = y;
}
public static void main(String args[]) {
MyClass m = new MyClass();
System.out.println("hello");
}
}
1 is an int literal, so MyClass(int x) is chosen.
Even if you remove the MyClass(int x) constructor, MyClass(short y) won't be chosen. You'll get a compilation error instead, since 1 is not short.
You'll have to cast 1 to short - this((short)1); - in order for the MyClass(short y) to be chosen.
As an addition to others answer I can suggest you to check which constructors are being called when you initialize variables of other types using the same literal:
short s = 1;
int i = 1;
And then check which constructor of MyClass is being called as you call them with above arguments.
I'm trying to compile this code
public class Foo {
static final int x = 18;
public void go(final int y){
System.out.println(x);
}
}
public class Mixed2 {
public static void main(String[] args){
Foo f = new Foo();
f.go(11);
}
}
And it is compiled. And even gives the result (18)
But this does not have to be. Why is this happening?
I use the idea
Thanks
The fact is, You cannot change the value of a final... However, you are not changing any final numbers, and if you were, you would get compiler errors, not exceptions.
It is important to know the differences between static and final:
Static makes a variable the same between all classes- Changing go() to static will still allow you to access it, but making go() static and removing static from x will prevent you from referencing x, because the go() function does not know which Foo class to find x in.
Final Makes a variable unchangeable- I do not know of any performance reasons for this, but mostly for constants.
For example, you do not want to be able to set the value of Boolean.TRUE to false
public class Foo {
static final int x = 18;
public void go(final int y) {
// This is not possible because 'x' is final,
// however the state of 'y' does not matter,
// because its value is not being changed
x = y;
System.out.println(x);
}
}
public class Mixed2 {
public static void main(String[] args){
Foo f = new Foo();
f.go(11);
}
}
From what I understand, you wonder why the code is valid in the following cases, when you expect it to throw an error.
In this case, you are not changing the field x, but simply adding a new variable with the same name, that override (shadows) the field x:
public class Foo {
static final int x = 18; // field x
// This creates a new variable x which hides the field x above
public void go(final int x /* variable x */) {
System.out.println(x);
}
}
In these next two cases, you are trying to change x, which will result in an error:
public class Foo {
static final int x = 18; // field x
// The field x is final and cannot be changed.
public void go() {
x += 1;
}
}
public class Foo {
static final int x = 18; // field x
// The variable x is final and cannot be changed.
public void go(final int x /* variable x */) {
x += 1;
}
}
Old Answer: If you're trying to have it print 11, you should call System.out.println(y) instead of using x.
Try following some Java tutorials and look closely at the code and at variable names.
I am trying to assign value to a class variable via a method. However, after the execution comes out of the scope of the method, the variable is still initialized to the default value. How do we accomplish this in Java?
I want to initialize x to 5 by calling the method hello(). I don't want to initialize by using a constructor, or using this. Is it possible?
public class Test {
int x;
public void hello(){
hello(5,x);
}
private void hello(int i, int x2) {
x2 = i;
}
public static void main(String args[]){
Test test = new Test();
test.hello();
System.out.println(test.x);
}
}
When you do
hello(5,x);
and then
private void hello(int i, int x2) {
x2 = i;
}
it seems like you might be trying to pass the field itself as parameter to the hello method, and when doing x2 = i you meant x2 to refer to the field. This is not possible, since Java only supports pass-by-value. I.e. whenever you give a variable as argument to a method, the value it contains will be passed, not the variable itself.
(Thanks #Tom for pointing out this interpretation of the question in the comments.)
The class property x is only visible by using this.x in hello() because you have declared another variable called x in the method's arguments.
Either remove that argument:
private void hello(int i) {
x = 5;
}
Rename the argument:
private void hello(int i, int y) {
x = 5;
}
Or use this.x to set the class property:
private void hello(int i, int x) {
this.x = 5;
}
You can create two methods.
public void setX(int a)//sets the value of x
{
x=a;
}
public int getX()//return the value of x
{
return x;
}
call setX to set the value of x and getx to return the value x.
Essentially these are called getter and setter and we use them to access private members from outside the class.