Is it possible to have an object which creates another copy of itself within it's constructor, while avoiding an infinite loop? [closed]

StackOverflow https://stackoverflow.com/questions/19308868

  •  30-06-2022
  •  | 
  •  

Question

I have zero reason to believe this is possible, but I'm trying to restore my ability to ask questions again, considering I am now in a position to contribute (4th year computer science major, working a software engineering job), compared to when I was a senior in high school, trying to play around with minecraft modding before I really knew Java.

Here is the code that initially wasn't working, and triggered my deciding to impose this question.

/**
 *
 */
package net.halalaboos.huzuni.console.commands;

import net.halalaboos.huzuni.Huzuni;
import net.halalaboos.huzuni.client.notifications.Notification;
import net.halalaboos.huzuni.console.Command;
import net.halalaboos.lib.io.FileUtils;
import net.minecraft.src.GuiPlayerInfo;
import net.minecraft.src.StringUtils;
import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.TwitterException;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TwitterAlerts implements Command {
    public Status tweet;
    public String tweetalert;
    public ConfigurationBuilder cb = new ConfigurationBuilder();
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    TwitterAlerts object = new TwitterAlerts();
    TwitterAlerts()
    {
        setkey();
        checkstatus();
    }
    public void setkey()
    {
        setkey();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("5c8ZcMgQihdS5kzGun9iSw");
        cb.setOAuthConsumerSecret("aRUUbQnDu5yAjmdI23LdjRu6vDtPWaKhc6dFVklne0");
    }
    List<String> twitters = new ArrayList<String>();
    List<String> pasttweets = new ArrayList<String>();
    /**
     * @see net.halalaboos.huzuni.console.Command#getAliases()
     */
    @Override
    public String[] getAliases() {
        return new String[] {"twitteralerts", "tfollow"};
    }

    /**
     * @see net.halalaboos.huzuni.console.Command#getHelp()
     */
    @Override
    public String[] getHelp() {
        return new String[] {"Do /twitteralerts <name of tweeter> to be alerted of new tweets!"};
    }

    /**
     * @see net.halalaboos.huzuni.console.Command#getDescription()
     */
    @Override
    public String getDescription() {
        return "In game alerts of new tweets on followed accounts.";
    }

    /**
     * @see net.halalaboos.huzuni.console.Command#run(java.lang.String, java.lang.String[])
     */
    @Override
    public void run(String input, String[] args) {
        twitters.add(input);

    }
    public void checkstatus()
    {
        while(twitters != null)
        {
            try {
                for(int i=0;i<twitters.size();i++)
                {
                    tweet = twitter.getUserTimeline(twitters.get(i)).get(0);
                    if (pasttweets.contains(tweet.getText()))
                    {

                    }
                    else
                    {
                        Huzuni.notificationManager.add(new Notification("Tweet Retrieved", tweet.getText()));
                        pasttweets.add(tweet.getText());
                    }
                }


            } catch (TwitterException e) {
                e.printStackTrace();
            }

        }
    }
}
Was it helpful?

Solution

Your class has a field

TwitterAlerts object = new TwitterAlerts();

when you initialize this field, it creates a new object, which creates a new object, ad nauseam.

Every TwitterAlerts object has a TwitterAlerts object which has a TwitterAlerts which has a TwitterAlerts...Get the idea?

Get rid of that field. You aren't even using it.

OTHER TIPS

look at these lines

TwitterAlerts object = new TwitterAlerts();
TwitterAlerts()
{
    setkey();
    checkstatus();
}

when you create an instance of TwitterAlerts, it wil create another instance of it. and then another create an instance of it....so you know why?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top