CleanBackupFolders.java file:

import java.io.*; public class CleanBackupFolders { public static void main(String[] args) throws IOException { ... Settings IniFileObj = new Settings(); ... } } 

File Settings.java:

 public class Settings { public void Settings() { System.out.println("Constructor executed!"); } } 

In the first class, the constructor is not called for an object. If after the line

 IniFileObj.Settings(); 

then everything in this constructor is executed. What is the problem?

    1 answer 1

    There are several problems in your code:

    1. You have not created a constructor. You created a method with the same name as the constructor, but gave it a return value, but the constructor does not have it.

    2. Methods in Java and (variables too) are usually called with a small letter. It is precisely because of this that you made a mistake - the constructor differs from other methods not only by the absence of a return value, but also by the fact that its name begins with a capital letter (more precisely, it completely coincides with the name of the class).

    Those. in fact, you first call the default constructor, which is created automatically if you have not created a. -l. another constructor. After that you call the usual method you created. And since you did not create a variable by naming conventions, i.e. with a capital letter, at first glance, your method looks static at all.


    Morality:

    If you respected the convention of naming, then you would not have an error.

    • 2
      Removed void - earned! Thank you for the clarification. As for naming, I am aware, I just missed it, I will correct it. - maotm
    • On the second point - the name of the constructor is written with a capital letter, so everything is correct here, here is the name of the instance - yes, it did not follow the agreements. - maotm