Why did I have to specify Solution.A = Integer.parseInt etc......
instead of just being able to specify A = Integer.parseInt etc...... ?
A is a public Solution wide variable.
Also, why did they put in public static final int MIN = min(A, B); if you can't just print MIN (which already did the calcultation), but have to modify it to call the function yourself ?
My task works, but this is nonsensical ?
Resolved
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
yz Backend Developer
16 July 2019, 14:30
1. look bro, they put " final " to make you think if there were final methods how could you solve .
2. in previous lesson they said that the " static " modifiers red by compiler one by one from top to bottom so
when there is " final " modifier it cant be changed and it red by compiler first before your static block
3. if you want your code works you should put " static block" before final methods .
Hope you got it.
+2
Davedabull
18 February 2020, 14:11
thank you for the explanation, i didnt notice the "final"
0
Henk
23 May 2019, 07:45
in my code below, if I take "Solution" away in front of "A = read number from user" or "B = number from user", it doesn't compile. I have to specify the class name, although I am in the same class. Also, I have to call the "min" method in the MAIN method, to get the result of a and b. Why can't I just return the variable in CAPS, "MIN", which is defined as "public static final int MIN = min(A,B)" and hence already calls the "min" method if you invoke it by calling it ? I didn't debug to see if it would invoke it, so maybe it doesn't, but then I don't understand the purpose of defining it as MIN = min(A,B) if it anyways doesn't calculate it automatically. Hope I make sense ?
public class Solution {
public static int A;
public static int B;
public static final int MIN = min(A, B);
static{
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Solution.A = Integer.parseInt(reader.readLine());
Solution.B = Integer.parseInt(reader.readLine());
reader.close();
}
catch (IOException e){
}
}
public static void main(String[] args) {
System.out.println(min(Solution.A,Solution.B));
}
public static int min(int a, int b) {
return a < b ? a : b;
}
}
0
Thomas Sixberry
14 February 2020, 16:10
Have you tried putting
under your static block? 0
Thomas Sixberry
14 February 2020, 16:12
This worked for me with no problems.
+2
Guadalupe Gagnon
22 May 2019, 16:43
Static variables (and methods) don't need an object to be called. Your code,
A = *read in number from user*
works fine while within the containing class (same with B and any other static variables you may have). The only time you would have to use Solution.A is if you were in a separate class (but both ways function exactly the same while within the containing class).
I don't know what you mean with the MIN variable. I would have to see your final code to see what you did.
0