为什么我不能在任何方法之外创建对象

时间:2016-12-02 09:42:41

标签: java class object aggregate-functions

在编写程序时,我遇到了一个stackoverflow错误,其中我在一个方法之外实例化,所以我在互联网上搜索它我不明白我的代码的任何正确的解决方案。

所以任何人都可以让我明白我的困惑。 我正在使用聚合,因为它是一个Has-a关系  下面是我的Mainpage.class,它将首先执行..

 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();
}
}

下面是我的People.class代码,将从Mainpage类的输入中选择好友调用

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();
        }

    }

我收到Stackoverflow错误,所以请清除我的怀疑..

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)

1 个答案:

答案 0 :(得分:5)

People ps = new People();类中删除People,因为它会导致对People的构造函数的无限调用。

调用new People()将初始化实例变量,因此它将以递归方式永久调用People()