简体   繁体   中英

Why can't I create object outside any method

While writing a program I came across a stackoverflow error in which I was instantiating outside a method, so I searched it on the internet I didn't understand any proper solution for the my code.

So can anyone please make me clear out of my confusion. I am using aggregation since its a Has-a relationship Below is my Mainpage.class which will execute first..

 import  java.util.Scanner;
public class Mainpage {
    Scanner in = new Scanner(System.in);
    People people;
public void openapp()
{
    people = new People();
    System.out.println("Welcome to the Chat");
    System.out.println();
    System.out.println("1. Newsfeed");
    System.out.println("2. Chat");
    System.out.println("3. Friends");
    System.out.println("Enter your choice");
    int input = in.nextInt();


    switch(input)
    {
    case 1:
        System.out.println("Newsfeed");
        break;
    case 2:
        System.out.println("Chat");
        break;
    case 3:
        System.out.println("Friends");
        break;
        default :
            System.out.println("Invalid Input");
    }
    if(input == 3)
    {
        people.friends();
    }
    }
public static void main(String args[]){
    Mainpage m = new Mainpage();
    m.openapp();
}
}

Below is my People.class code which will be called on selection of friend from input of Mainpage class

import java.util.Scanner;
public class People{
    People ps = new People();
    Scanner input = new Scanner(System.in);
    public void friends()
    { 
        System.out.println("Your friends are");
        System.out.println();
        System.out.println("Amit");
        System.out.println("Rahul");
        System.out.println("Ankita");
        System.out.println("Enter the friend name to see info");
        String fr = input.nextLine();
        //ps.friend(fr);

        switch(fr){
        case "Amit": 
            System.out.println("Name: Amit");
            System.out.println("DOB: 09-02-1993");
            System.out.println("Age = 23");
            System.out.println("Sex: M");
            break;
        case "Rahul":
            System.out.println("Name: Rahul");
            System.out.println("DOB: 11-10-1993");
            System.out.println("Age = 23");
            System.out.println("Sex: M");
            break;
        case "Ankita":
            System.out.println("Name: Ankita");
            System.out.println("DOB: 12-03-1993");
            System.out.println("Age = 22");
            System.out.println("Sex: M");
            break;
        default:
            System.out.println("You have no one With name "+fr+" in your contact list");
            System.out.println("Please again select the friends form the lists");
            ps.friends();
        }

    }

I am getting Stackoverflow error so please clear my doubt..

Exception in thread "main" java.lang.StackOverflowError
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)
        at People.<init>(People.java:4)

Remove People ps = new People(); from your People class, because it will cause infinite calls to the constructor of People .

Calling new People() will initialize instance variables, so it will recursively call People() forever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM