'Can the object of a class having a constructor be created?

Given this class:

public class MyClassOne {

private World world;
  

  public MyClassOne(World world) { 
    this.world = world;
  }

public void myMethod() {
    //do something;
  }
}

I tried creating an object MyClassOne in MyClassTwo:

Public class MyClassTwo{
  
MyClassOne mco = new MyClassOne();  // error thrown on this line

public void myMethod2() {
    //do something
   
  }
}

I gpt a compilation eror:

'MyClassOne(config.World)' in 'mypackage.MyClassOne' cannot be applied to '()

What am I doing wrong? How can I successfully create an object of MyClassOne given it contains a constructor?



Solution 1:[1]

You need to pass a World reference to the constructor to create a MyClassOne. That's what your constructor is declared to need.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bmargulies