'No enclosing instance of type savings is accessible in java

im just getting message like "No enclosing instance of type savings is accessible." starting in line 17. I don't know what kind of instance that the IDE means. I'm learning java for 2 weeks now.

public class savings {
class MyInfo{
    String name;
    int balance;
}
class MyParent{
    String name;
    int balance;
}
class Mysister{
    String name;
    int balance;
}
public class Mainsimulationsavings {
public static void main(String[] args) {
    
    MyInfo me = new MyInfo();
    me.name ="gaga";
    me.balance =1000000;
    
    MyParent parent = new MyParent();
    parent.name ="Abdul";
    parent.balance =20000000;
    
    Mysister sis = new Mysister();
    sis.name ="Ifdah";
    sis.balance =500000 ;
    
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "No", "No.ID", "balance", "name");
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "--", "----", "-----", "----");
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "1", "12345", "Rp."+me.balance,me.name);
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "2", "54321", "Rp."+parent.balance,parent.name);
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "3", "67890", "Rp."+sis.balance,sis.name);
}
}

}



Solution 1:[1]

Sticking classes in classes makes 'instance classes' which you don't want, usually. They have an extra invisible field of their outer type: You can't make new instances of e.g. Mysister here without also having an instance of savings available, which you don't. This is all very confusing so you never want to do this until you're much more advanced in java (and even then, I generally avoid it, saving a few characters at the cost of confusing code is rarely a good tradeoff).

Either don't stick classes in classes, or remember to always mark classes-in-classes as static. Such as static class MyParent {.

Solution 2:[2]

The Answer by rzwitserloot is correct.

? The likely solution is to move those related classes to outside the Savings class.

Also, given your naming, you seem to be confusing the idea of classes with instances. There is no need for MyInfo and MyParent and MySister. The state and behavior of those are all the same. The only difference is their relationship to you. Instead, using a single class Person. And, either add a relationship member field to the class, or imply the relationship through the naming of instance variables. See code below.

Tip: Define your classes more briefly as a record if their main purpose is to communicate data transparently and immutably.

By the way, watch your naming. Java naming conventions start a class name with an uppercase letter. So Savings, not savings.

record Person ( String name , int balance ) {}

Another tip: A record can be declared locally, within a method. Or declare by nesting in a class. Or declare separately, in its own .java file.

Yet another tip: You can use a LOW LINE (underscore) character to arbitrarily group the digits of a numeric literal.

In your main method, or other such code, instantiate.

Person me = new Person( "Gaga" , 1_000_000 ) ;
Person parent = new Person( "Abdul" , 20_000_000 ) ;
Person sister = new Person( "Ifdah" , 500_000 ) ;

Make a list and loop, to report.

List<Person> persons = List.of( me , parent , sister ) ;
for( Person person : persons ) 
{
    System.out.println( "Name: " + person.name() … ) ;
}

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 rzwitserloot
Solution 2 Basil Bourque