Y
Y
Yaten2022-01-22 08:59:19
Java
Yaten, 2022-01-22 08:59:19

How to refer to an object from another class?

There is a class Circle

public class Circle extends Shape {
    private double radius;
    
    public Circle(String shapeColor, double radius) {
        super(shapeColor);
        this.radius = radius;
        }

    public double calcArea() {
        return Math.PI * Math.pow(radius, 2);
   }

    @Override
    public String toString() {
        return "This is Circle, color: " + shapeColor + ", radius =" + radius + "\nShape area is: " + calcArea();
    }
 public static void main(String[] args) {
        Circle kub = new Circle("GREEN", 10);
        System.out.println(kub);
    }
}

It has a kub

object, how can I access this object from another class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2022-01-22
Hasanly @azerphoenix

Good afternoon.
The kub object is located in the scope of the main method. This object cannot be accessed from another class.

How can I access this object from another class?

What result do you want to achieve with this?
I will share my opinion about the code itself:
Since your Circle class is a pojo, it is incorrect to place the main method in it.
Create another class and drop the main method there.
Those. it should turn out like this:
pseudocode

class Circle extends Shape {}
class Triangle extends Shape {}
public class Main { void main() }

If you follow your code, it will look like this:
class Circle extends Shape { void main() }
class Triangle extends Shape { void main() }
т.е. каждый pojo класс должен содержать точку входа, что странно...
И еще один совет, осознанно давайте названия переменным, классам, методам. 
А то <code>Circle kub</code> (Круг квадрат) ну сами понимаете.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question