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 1089595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:14:45+00:00 2026-05-16T23:14:45+00:00

Thanks for taking the time to read. =) The problem I am having is

  • 0

Thanks for taking the time to read. =)

The problem I am having is mainly Object visibility. I’m creating objects and attempting to create references to them before they have been initialized or have any data in them. You can get an idea what I’m trying to do here with the code provided.

Certain Squares can see other square etc. Now my code does compile! The issue is I have getter-methods that return null from these objects. I tried placing all the initializations in the Square-class declaration however that is where I get “Illegal Forward reference”.

So finally, at the end of the code block you notice how I attempt to access these objects and their references. All four return null, what I’m going for is some indication a reference exists.

I do understand slightly what is going wrong here, perhaps my approach with the objects is incorrect? I think my amateur skills are mainly what hold me back on this one =)

class Puzzle extends JFrame
{

     Square square1;
     Square square2;
     Square square3;
     Square square4;
     Square square5;
     Square square6;
     Square square7;
     Square square8;
     Square square9;
     Square square10;
     Square square11;
     Square square12;
     Square square13;
     Square square14;
     Square square15;
     Square square16;

    public Puzzle()
    {

        PuzzleListener plist = new PuzzleListener();
        JPanel puzzle_board = new JPanel(new GridLayout(4,4,5,5));

        //randomize the text index's and the starting black square.

        //            Square(north, south, east,west,isblack,text)
        square1 = new Square(null,square2,square5,null,false,"1");
        square2 = new Square(null,square3,square6,square1,false,"2");
        square3 = new Square(null,square4,square7,square2,false,"3");
        square4 = new Square(null,null,square8,square3,false,"4");
        square5 = new Square(square1,square6,square9,null,false,"5");
        square6 = new Square(square2,square7,square10,square5,false,"6");
        square7 = new Square(square3,square8,square11,square6,false,"7");
        square8 = new Square(square4,null,square12,square7,false,"8");
        square9 = new Square(square5,square10,square13,null,false,"9");
        square10 = new Square(square6,square11,square14,square9,false,"10");
        square11 = new Square(square7,square12,square15,square10,false,"11");
        square12 = new Square(square8,null,square16,square11,false,"12");
        square13= new Square(square9,square14,null,null,false,"13");
        square14 = new Square(square10,square15,null,square10,false,"14");
        square15 = new Square(square11,square16,null,square14,false,"15");
        square16 = new Square(square12,null,null,square15,false,"16");

}



class Square extends JPanel
{

    //visible lable
    JLabel lblText;

    //internal object ID
    int id;

    //Define the visible neighbors
    Square north;
    Square south;
    Square east;
    Square west;

    //is the square black
    boolean isBlack;

    public Square(String text)
    {

        lblText = new JLabel(text);
        //setLayout(new GridLayout(5,5));
        lblText.setLocation(10,10000);
        add(lblText);
    }

    public Square(Square n,Square e,Square s,Square w,boolean black, String text)
    {

        north = n;
        east = e;
        south = s;
        west = w;

        isBlack = black;

        lblText = new JLabel(text);
        //setLayout(new GridLayout(5,5));
        lblText.setLocation(10,10000);
        add(lblText);

    }//end square

    Square getNorthNeighbor()
    {
        return north;
    }
    Square getSouthNeighbor()
    {
        return south;
    }
    Square getEastNeighbor()
    {
        return east;
    }
    Square getWestNeighbor()
    {
        return west;
    }
    boolean getIsBlack()
    {
        return isBlack;
    }


}


System.out.println("Panel 1 has been clicked and has the neighbors " + "North: " + square1.getNorthNeighbor() + "East: " + square1.getEastNeighbor() + "South: " + square1.getSouthNeighbor() + "West: " + square1.getWestNeighbor());
  • 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-05-16T23:14:46+00:00Added an answer on May 16, 2026 at 11:14 pm

    Given that they need to know about each other, why not create all the squares first, and then have a setNeighbours(Square north, Square south, ...) method that you can call when you’ve actually created all the objects? It’s not nice in terms of immutability (generally a nice property for a type to have) but fundamentally, you’ve got circular references.

    To put it another way, imagine you had to create two Person instances, and indicate to each that they were brothers. You can’t do:

    Person billy;
    Person bobby;
    billy = new Person(bobby);
    bobby = new Person(billy);
    

    but you can do:

    Person billy = new Person();
    Person bobby = new Person();
    billy.setBrother(bobby);
    bobby.setBrother(billy);
    

    It’s nicer if you can avoid requiring circular references in the first place, but sometimes that’s just the way of things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.