If I have a method e.g.
public int addition(int x, int y)
{
int z = x + y
return z;
}
public int usez(int z)
{
// TODO
}
public static void main(String [ ] args)
{
classname d = new classname();
d.usez(z)...
}
z is not readable. So, is there a way to make the resulting z from the first method into the usez method when testing it in main =/
hopefully this makes some sense! in a real version e.g. if x,y = 1,2 then z = 3 I want the main method to recognise z to be 3.
z is a private local variable for the method addition. You can't and should not attempt to access it. However, it's value is returned by the function it's defined in (addition). Therefor, you can do the following:
classname d = new classname();
int zValue = d.addition(1,2);
d.usez(zValue);
Assuming that all methods including main() are in same class, You can make z as class member variable as
int z= 0;
and assign value to it in
public int addition(int x, int y)
{
z = x + y
return z;
}
and use in usez(z).
public static void main(String [ ] args)
{
classname d = new classname();
d.usez(z)...
}
Related
I'm new to Java and wanted some clarification, I understand that I'm declaring an int variable of x inside the method's parameters but why is it that 'result' can not be resolved to a variable.
public class Methods{
public static void main(String[] args) {
//f(x) = x * x
square(5);
System.out.println(result);
}
//This method
static int square(int x) {
int result = x * x;
}
You can, but note that local variable are only defined in their respected functions. So even though result is defined in square(), it's not defined in main(). So what you want to do is return a value for your square function and store that in a variable inside main() like so:
public static void main(String[] args) {
int myResult = square(5);
System.out.println(myResult);
}
//This method
static int square(int x) {
int result = x * x; // Note we could just say: return x * x;
return result;
}
Example Here
Since you are beginner, I will explain it bit thoroughly
Rule 1:Local variables are declared in methods, constructors, or blocks.
Rule 2:Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
Rule 3:There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.
public class Methods{
public static void main(String[] args) {
//f(x) = x * x
square(5);
System.out.println(result); //result! who are you?
//Main will not recognize him because of rule 3.
}
static int square(int x) {
int result = x * x; //you did it as said in rule 1
}//variable result is destroyed because of rule 2.
Please go thorough comments in code.
Well solution for your code is:
public class Methods{
public static void main(String[] args) {
//f(x) = x * x
int result=square(5);
System.out.println(result); //result! who are you?
//Main will not recognize him because of rule 3.
}
static int square(int x) {
int result1 = x * x; //you did it as said in rule 1
return result1;
}
If you are not allowed to initialize a final non-static data member twice, then how can I set x to something that I want in the following example?
class Temp6
{
final int x;
Temp6()
{
System.out.println(this.x);
this.x=10;
}
public static void main(String[]s)
{
Temp6 t1 = new Temp6();
System.out.println(t1.x);
}
}
Java by default gives x a value of 0, so how can I change it to 10?.
A variable marked final in Java can only be initialized once.
Simply declaring x with final int x; does not initialize it. Therefore, it is legal to assign to x in the Temp6 constructor. However, you would not be able to assign a different value to x after the constructor.
That is, the assignment to t1.x in the following:
public static void main(String[] s) {
Temp6 t1 = new Temp6();
t1.x = 11; // ERROR
}
is not legal.
Initialize final variables in the class constructor.
public class Blam
{
private final int qbert;
public Blam(int qbertValue)
{
qbert = qbertValue;
}
}
Reading this.x in your code should give an error, because final variables are not initialized upon declaration. t1.x should be 10 because x is definitely assigned at the end of the sole constructor.
You have to swap the two lines in the constructor for it to compile and it will be 10 there.
class Temp {
int x; // declaration and definition; defaulted to 0
final int y; // declaration, not initialized
Temp() {
System.out.println(x); // prints 0
x = 1;
System.out.println(x); // prints 1
x = 2; // last value, instance.x will give 2
System.out.println(y); // should be a compiler error: The blank final field y may not have been initialized
y = 3; // definite assignment, last and only value, instance.y will be 3 whereever used
System.out.println(y); // prints 3
y = 4; // compile error: The final field y may already have been assigned
}
}
I never thought about this before, interesting point here.
Final field variables behave like local variables in methods, they must be explicitly assigned before usage (definite assignment is hard to formalize, see JLS reference, but it's quite logical).
If you want to give a value to x from outside, you could do it like this:
public class Temp {
private final int x;
public Temp(int x) {
this.x = x;
}
public int getX() { return this.x; }
public static void main(String[] args) {
Temp temp = new Temp(10);
System.out.println(temp.getX()); // 10
}
}
final variable are java constants. They should be initialized before class loads.
final int x=10;
If your final variable is static then it's not like you have to give the value at the declaration itself, you can have something like -
class Demo {
static final int x;
static {
x = 10;
}
}
static block gets executed only once, at the time of class loading
Can you help me find my error?
I'm trying to use these two methods here but my output is not working.
class Nine {
public static void Nine(String[] args) {
int x,y,z;
y = 3;
x = 7;
z = addEm(a, b);
System.out.println("answer= " +x);
}
public static addEm (double a, double b){
int c;
c = a+b;
}
}
Actually there are a lot of error in your code:
z=addEm(a, b);
here a and b are meaningless, you should use z=addEm(y,x); (if your intent is to sum three with seven)
System.out.println("answer= " +x);
I guess that you want to show the the results of the sum, therefore you should print z (and not x), so you should substitute with System.out.println("answer= " +z);
public static addEm (double a, double b) {
Here you missed the return type, and you need to consider also the type of parameters a and b. Since y,x and z are int, it is better if also a and b are int, and therefore specify also the return type as int:
public static int addEm (int a, int b) {
Or you can declare everything (y,x,z,a,b and return type) as a double: the important here is that they should be all of the same type. Moreover you miss also the return statement of the function addEm, that summarizing becomes:
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
And finally also the function
public static void Nine(String[] args)
it is not right named for an entry point: its names should be main.
So in conclusion, if you apply all the fix (by modifying as less as possible your original code) a code that compile, run and works following some 'logic' is:
class Nine {
public static void main(String[] args) {
int x, y, z;
y = 3;
x = 7;
z = addEm(y, x);
System.out.println("answer= " + z);
}
public static int addEm(int a, int b) {
int c;
c = a + b;
return (c);
}
}
Man, this is a very basic java lesson:
every prog need an entry point, which is in java:
public static void main(String args[]){}
And then your code will execute.
You're passing arguments a and b to addEm, but those variables aren't initialized. I'm expecting you wanted to pass x and y instead.
class Nine
{
public static void Nine(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " +x);
}
public static addEm (double a, double b)
{
int c;
c=a+b;
}
}
Your code will not work because your addEm method does not have any return type. In addition, the method you wrote takes Double params but while using you are trying to pass int to it. You also do not have any main method. I am assuming you misspelled or misunderstood the main method so below is the code which should work
class Nine
{
public static void Main(String[] args)
{
int x,y,z;
y=3;
x=7;
z=addEm(x, y);
System.out.println("answer= " + x);
}
public static int addEm (int a, int b)
{
int c;
c=a+b;
return c;
}
}
I have this code:
class Foo {
int x = 12;
public static void go(final int x) {
System.out.println(x);
}
}
The argument final x and the instance x have the same name. How would I refer to the instance variable x = 12 if I want to use it in the go() method considering its name is the same with the argument variable?
You need to make it static in order to use it within static method:
static int x = 12;
Then you can get a reference to it via the class name:
public static void go(final int x)
{
System.out.println(Foo.x);
}
Or alternatively, create an instance and use it locally:
int x = 12;
public static void go(final int x)
{
Foo f = new Foo();
System.out.println(f.x);
}
Or use instance method, and refer to the instance x with the keyword this:
int x = 12;
public void go(final int x)
{
System.out.println(this.x);
}
this.x points to the instance variable.
In order to refer to an instance variable, you have to be in a real instance: your method should not be static then.
I have written a piece of code :
public class Child{
int y ;
private static final int z = getZ();
static {
System.out.println("The value of z is "+z);
}
public int getX(){
System.out.println("get x");
return 10;
}
public int getY(){
Child ch = new Child();
System.out.println("get y");
ch.y = getX();
return y;
}
public static int getZ(){
System.out.println("get z");
return new Child().getY();
}
public Child(){
System.out.println("Child constructor");
}
public static void main(String...args){
Child ch = new Child();
System.out.println("the value of z in main is "+z);
}
}
And the output is :
get z
Child constructor
Child constructor
get y
get x
The value of z is 0
Child constructor
the value of z in main is 0
Can anyone please explain me why the value of z is 0 and not 10 ?
EDIT:- Thanks everyone , I got the answer to my first question . I still have a doubt , as far as I know the static blocks are executed after the class is loaded and before the first object of the class is instantiated . Well then the SOP("The value of z is "+z) should have been executed before SOP("Child constructor") ! Ain't it ?
Look at getY():
public int getY(){
Child ch = new Child();
System.out.println("get y");
ch.y = getX();
return y;
}
The first three lines are irrelevant - they don't change the value of y in this instance, which is what gets returned.
You're creating an awful lot of pointless objects in frankly spaghetti code, called while initializing the same class that you're constructing instances of. I suggest you try to keep your code a lot simpler than this. Static initializers should be avoided where possible to start with, let alone ones that go all round the houses to do no useful work.
because getY() sets ch.y to 10, but returns the value of this.y.
In GetY you return this.y which never gets set.
On getY() you returned the uninitialized y variable instead of the Child ch instance.
Its because inside getY() method you are constructing a new CHild object and you are assigning 10 to that instance of variable y and when you return y that will be the current instance variable y.
I hope it explains .
To your second point, static fields and static blocks are initialized / executed in textual order.
Eg:
public class StaticTest()
{
static
{
System.out.println("s1 in block 1 :" + s1);
}
private static String s1 = "s1";
static
{
System.out.println("s1 in block 2 : " + s1);
}
public static void main(String args[])
{
new StaticTest();
}
}
The output of this will be -
s1 in block 1 :
s1 in block 2 : s1