Java variable scope in and outside loop [duplicate] - java

This question already has answers here:
Java variable may not have been initialized
(7 answers)
Why does the Java compiler not understand this variable is always initialized?
(3 answers)
Closed 2 years ago.
I wrote this code in an online java test recently. It was inside a method that was set to return an integer. I got an error message something like "variable a has no value assigned to it". I find this odd because the forloop must have access to the methods variable and the assignments inside of the loop must be registered right?
int a;
for(int i=1;i<5;i++){
a = i;}
return a;
I did assume that the method would return the integer 5.
This is only a question regarding scope of the variable a. I know that the code makes no sense.

You can try below code , that will help you. Initialisation is mandatory for any variable to return or hold any value.
class a{
public static void main(String[] args) {
System.out.println(test());
}
public static int test(){
int a = 0;
for(int i=1;i<5;i++){
a = i;}
return a;
}
}

Related

"Which variable(instance or local) is in action in the public method of foo class?" [duplicate]

This question already has answers here:
What is variable shadowing used for in a Java class?
(5 answers)
Is "this" shadowing a good idea?
(10 answers)
Closed 3 years ago.
It seems like its using local variable of the method!!!! I thought it would give an error on call non static variable on static method but it did not.
public class foo{
int x = 12;
public static void go(final int x){
System.out.println(x);
}
}
Actually it doesn't have a error which is interesting..
System.out.println(x)
will print the parameter of the method go
If you want to access field of the class, use this keyworld (if your method not static):
System.out.println(this.x)
In your case, you need to have an instance of foo class and use
foo f = new foo();
System.out.println(f.x);

Static recursive method in Java doesn't update variable value [duplicate]

This question already has answers here:
How do I pass a primitive data type by reference?
(8 answers)
Closed 4 years ago.
I have the following code
private static void modifyX(int x) {
if (x!=0) {
modifyX(--x);
}
}
I want the value of my variable to be updated after the recursive call, so I wrote the following code:
public static void main(String... args) {
int x = 5;
modifyX(x);
System.out.println("Modified value:\t" + x);
}
However, the value remains the same (5).
Why is my variable not updating?
You are not passing the same instance of the value 5. Instead, the JVM is creating a new int with a value of 5, and passing that to your method.
See this thread for more information.

Return value of a void function [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.. If it doesn't return a value than it is void..
However in the below program, I'm able to change the values of an array from a void function. Can anybody explain this to me please?
public class ArraysInMethods {
public static void main(String args[]){
int rd[] = {2,3,4,5,6};
change(rd);
for(int y: rd){
System.out.println(y);
}
}
public static void change(int x[]){
for(int counter = 0; counter<x.length;counter++){
x[counter]+=5;
}
}
}
I'm learning Java, and from what I learned, is that you need to specify the value that a function return.
That is correct only for methods returning values, i.e. methods other than void. These methods define expressions, while void methods define statements.
Calling your change method is a statement, in the sense that it lacks return values. It does not mean, however, that it cannot change the state of your running program - for example, give different values to variables.
However in the below program, I'm able to change the values of an array from a void function.
You are not returning a value from your void function; all you do is modifying the array in place. This is allowed, because arrays are passed by reference.

Why is this printing out 1 instead of 2? How can I improve? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
Given this code:
public class Test {
public void add(int x){
x++;
}
public static void main(String args[]){
Test t = new Test();
int a = 1;
t.add(a);
System.out.println(a);
}
}
I just want to print out 2 instead of 1. I think I am calling this method wrong.
Could you help me understand why?
java passes by value , that means your variable got serialized and its value was sent to the method.
To print 2 you need to make your method returns the value after increment .
Your method is declared void so it doesn't return anything. Variable a will always remain unchanged when using it for calling the method add.
It should be, for your purpose:
public int add(int x){
return x++;
}
There are a lot of errors in your code, and you should study Java properly. It shows you haven't understand neither the unary operators, that's not strictly related to Java.
However one of the possible solutions (there are many), is to change your code as follow:
public class Test {
public int add(int x){
return ++x;
}
public static void main(String args[]){
Test t = new Test();
int a = 1;
a = t.add(a);
System.out.println(a);
}
}
Again, study properly Java before trying any exercise or posting on SO.
Next time, before posting a new question please review how to ask.

scope of variable inside main in Java? [duplicate]

This question already has answers here:
Why does Java not have block-scoped variable declarations?
(6 answers)
Closed 9 years ago.
Am I correct that a block inside main() doesn't have separate scope?
For example, if I do the following, I will get compile error:
static int i = 1;
public static void main(String[] args) {
int i = 2;
{
int i = 3;
}
}
Why is this?
When I try to compile this, I get this message:
error: variable i is already defined in method main(String[])
This means that the static int i = 1; does not cause the error. The scope of the first i in main is for the whole main, so when you try to make another one, in the code block, you get an error.
Note that if you declared the i in a block:
public static void main(String[] args){
{
int i = 2;
}
{
int i = 3;
}
}
You don't get a compile error (see here).
The static int can be accessed in one of two ways: this.i (not recommended) or ClassName.i (recommended for accessing of a static variable)
Java does not allow obfuscation of stack (ie. local variables). You can obfuscate an instance or class variable because there are other ways to access them (ie. this.i for instance, or MainClass.i for static).

Categories