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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:43:53+00:00 2026-05-17T21:43:53+00:00

I cant seem to find an answer on the internet anywhere. So I have

  • 0

I cant seem to find an answer on the internet anywhere.

So I have a processing project in Eclipse

I want to be able to access objects in my “main” class that extends PApplet in another class without having to pass them through the constructor. I am not the most experienced programmer so my terminology might be off but hopefully this example clarifies what I am trying to do.

so for


public class Main Extends PApplet{

    //Example Class to access objects from main
    Interaction interaction;   

    //Example Objects I want
    boolean someObject;

    void setup(){        
        someObject = true;
        Interaction = new Interaction();  
    }

    void draw(){
    }   
}

public class Interaction{

    PApplet p;

    public Interaction(PApplet parent){
        p = parent;
    }

    public void mousePressed(){
        if(someObject){
            //do something
        }   
    }
}  

so I know I could pass that object into the constructor of Interaction like

PApplet p;
boolean o;

public Interaction (PApplet parent, boolean SomeObject){
    p = parent;
    o = someObject;
}

but this gets a little crazy in an example like this where I just want to hold all of my mouse and keyboard interaction in its own class because its getting huge, but I have run into the need to this time and time again and cant seem to figure it out.

  • 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-17T21:43:54+00:00Added an answer on May 17, 2026 at 9:43 pm

    What you are describing is called a getter method.

    You can add a getter method to your Main, but first read this -> http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html

    Here is the example.

    public class Main Extends PApplet {
    
        ...
    
        public boolean getSomeObject ( )
        {
            return someObject;
        }
    }
    
    public class Interaction {
    
       PApplet p;
    
       public Interaction(PApplet parent){
          p = parent;
       }
    
       public void mousePressed() {
    
           if( p.getSomeObject( ) ) {
               //do something
           }
    
       }
    }
    

    BTW, boolean is not an Object in Java, Boolean is.

    EDIT
    After your comment I DO understand what you need.

    You need to create an interface and pass this interface to Interaction constructor.

    This interface should provide all the method that your Interaction needs. If it needs an access to PApplet, then that’s what your interface should provide.

    With this in mind, here is a new hierarchy:

    public interface IInteractionContext
    {
      boolean getSomeObject( );
    }
    
    public class Main
      extends PApplet
      implements IInteractionContext
    {
      @Override
      public boolean getSomeObject ( )
      {
        return someObject;
      }
    
      ...
    }
    
    public class Main Extends PApplet {
    
        ...
    
        public boolean getSomeObject ( )
        {
            return someObject;
        }
    }
    
    public class Interaction {
    
       final IInteractionContext ctx;
    
       public Interaction(IInteractionContext ctx)
       {
          this.ctx = ctx;
       }
    
       public void mousePressed() {
    
           if( ctx.getSomeObject( ) )
           {
               //do something
           }
    
       }
    }
    

    With these changes, Interaction could care less if ctx is Main, PApplet or Unicorn. Its constructor requests the expected behavior and your implementation of Main provides this behavior at runtime.

    In any case read the javaworld article and related articles.
    Also, read articles about Dependency Injection

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

Sidebar

Related Questions

I have a problem which I cant seem to find answer to through searches
I have been searching for a few hours and cant seem to find answer
Been trying to google an answer but cant seem to find anything, I have
I can't seem to find the answer to this anywhere... I have a function
Hi people i have looked around but cant seem to find the answer i'm
I have a question that I cant seem to find an answer on. This
I can't seem to find an answer on this and just want to make
Simple question which I can't seem to find an answer of: I have two
I cant seem to find any answer to this, other than a series of
I have been looking around for a while, and cant seem to find the

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.