Java problem:
I'm a noob to Java and I wanted to try using multiple classes, parameters and return; method. However my program returns always "0" back to me, instead of the number I feeded there...
Here's my code:
Spoiler :
import java.util.Scanner;
public class parametrit {
static Scanner skan = new Scanner(System.in);
static int n = skan.nextInt();
static apuluokka otus= new apuluokka();
public static void main(String args[]){
otus.AsetaLuku(n);
otus.tulo();
}
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
public class apuluokka {
private static int numero;
public void AsetaLuku(int luku){
luku=numero;
}
public static int palauttaja() {
return numero;
}
public void tulo() {
System.out.println("Luku on "+ palauttaja());
}
}
Can you help me?
Edit: Sorry for weird class/ variable names!
Oh sweet, you are the bossI have not looked in detail, but I suspect your problem is with:
public void AsetaLuku(int luku){
luku=numero;
}
This will NOT make the class variable numero equal the passed variable luku but vice versa. I think you want:
numero=luku;
A more usual pattern for this would be:
public void AsetaLuku(int numero){
this.numero=numero;
}
Yeah x=y in most computer languages is not a statement of equality or equivalence but one of assignment: you are taking the variable "x" and assigning it the value of "y"; or put another way, you are setting x to whatever y currently is. I was taught that "=" meant "becomes equal to", rather than simply "equals".
In some languages, the assignment symbol is the same as the comparison symbol, but in other languages they are different.
Yeah, that's right! In Java, don't you test the equality with == mark? By the way, you guys are so helpful so I have another question: what is the meaning of static statement? I don't know when to use it and what it actually does. My Eclipse won't run the program sometimes, if I don't use static- word in specific method/var. Thanks.
Your program probably isn't working without declaring the variables as static because you're changing a variable in one instance of the class and expecting it to change in another instance of the same class. If this is what you want then you need to declare it as static.
My Eclipse won't run the program sometimes, if I don't use static- word
Is the English word "static" part of the confusion?
Is it better to think of Static as meaning "status-ly" or "state-ly" instead of "unchanging"?
for (initialization; termination;
increment) {
statement(s)
}
When using this version of the for statement, keep in mind that:
The initialization expression initializes the loop; it's executed once, as the loop begins.
When the termination expression evaluates to false, the loop terminates.
The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.