Answer the question
In order to leave comments, you need to log in
Why doesn't output and show in red in Java?
Just started learning Java
package com.company;
public class Main {
public static void main(String[] args) {
class Square {
Square(int i) {
int x =i ;
}
}
class TestSquare {
Square S1 = new Square(10);
System.out.println(S1.x);
}
}
}
Answer the question
In order to leave comments, you need to log in
One main advice can be given to the author:
Take the resource you are currently working on, put it out of your head and never remember it again. The code is cosmically bad, and not because you are a beginner, but because you are taught not to understand what.
Classes in Java serve a purpose, for what purpose your classes are created is completely incomprehensible, except that, as mentioned above, creating classes in main is nonsense
// Class declaration
public class Test {
// number exponentiation
public int squareNumber(int i) {
return i *= i;
}
// factorial
public int factorial(int i) {
return (i <= 1) ? 1 : (i * factorial(i - 1));
}
// main method
public static void main(String[] args) {
Test test = new Test(); // new instance
System.out.println(test.squareNumber(5)); // 25
System.out.println(test.factorial(5)); // 120
}
}
Firstly, at first it is better to read the very basics than to already climb with stupid questions.
Secondly, edit your code by adding the tag
PS
1. Move the variable x from the constructor to the class level
2. There is no println8 method
3. In general, creating classes in the main method is a complete game
Because in the presumably function
Square(int i)you're assigning x = i instead of squaring i and storing the result in x.
Good question, not stupid at all. The question is obviously being asked by a newbie, and he's already been splattered with toxins.
It makes no sense to explain the error, it makes sense to advise you to review the videos on the basics of java.
To do this: Square.x
you need to learn what class fields are.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question