Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8742425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:29:51+00:00 2026-06-13T11:29:51+00:00

public class TestClass(){ public static void main(String []args) { TestClass t1 = new TestClass();

  • 0
public class TestClass(){
    public static void main(String []args) {
        TestClass t1 = new TestClass();
        t1.anything();
    }
}

Is it not strange to create a object in the definition of same class? Because then in response – this object creates a new object, then this new object creates another, and the infinite loop begins to never end until the memory is full.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:29:51+00:00Added an answer on June 13, 2026 at 11:29 am

    Is it not strange to create an object in the definition of the same class
    than in response the object create a new object then this new object
    create another and the infinite loop begins

    No, the main method only runs once when you run your program. It will not be executed again. So, the object will be created only once.

    Think of your main method to be outside your class. Which creates an instance of your class, and uses the instance created. So, when you create an instance from main method, the constructor is invoked to initialize the state of your instance, and then when the constructor returns, the next statement of your main method is executed.

    Actually, you can consider main method not to be a part of the state of the instance of your class.

    However, had you created the instance of your class inside your constructor (say 0-arg), and the reference as instance reference variable, then that will turn into an infinite recursion.

    public class A {
        private A obj;
        public A() {
            obj = new A();  // This will become recursive creation of object.
                            // Thus resulting in StackOverflow 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: public class TestClass { public static void main(String[] args) { TestClass t =
public class MainClass { public static void main(String [] args) throws ClassNotFoundException{ //String[] xyz
I have this code: class Program { static void Main(string[] args) { TestClass instanceOfClass
I have public class TestApp { // TestApp.java public static void main(String[] args) throws
@Entity public class TestClass implements Serializable{ private Integer id; private Set<String> mySet; @Id @GeneratedValue
My jpa looks like below public class TESTClass implements Serializable { ... private String
public class TestClass { public TestClass(int id, string name) { Name = name; Id
Here is the code. public class Test { class InnerClass{ } public static void
Given I have a class with two constructors: public class TestClass { ObjectOne o1;
I have following data structure (simplified): A giant list of the class TestClass public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.