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

  • Home
  • SEARCH
  • 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 8814561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:09:28+00:00 2026-06-14T04:09:28+00:00

How do I create two or more classes that interact with each other? For

  • 0

How do I create two or more classes that interact with each other?
For example, a method in the first class will be static, producing a Fibonacci number for instance, and another method in the second class will do something with the Fibonacci number which has been created by the method in first class, and how do I extend my classes?

  • 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-06-14T04:09:29+00:00Added an answer on June 14, 2026 at 4:09 am

    Since it seems that you are beginning to code in java, I’d say that this oracle article about Modifiers are a good start to understand how a Class can interact with another.

    So to answer your questions:

    So how do i make 2 or more classes that interact with each other ?

    There are several ways for one class to interact with another one. Please be aware that I’ve selected the ones I’ve found more useful for your particular example. The most common of them are

    • Instance of Class Bar call a method from another instance of a Class Foo like the example below:

      Foo foo = new Foo()
      Bar bar = new Bar();
      
      bar.setSomeFieldValue(foo.getSomeOtherFieldValue());
      
    • Class Foo extends Class Bar and call a construct defined on it’s super class: This tries to answer your question on: How you extend a Class

      Class Foo extends Bar
      {
          public Foo()
          {
              super(); //Calling the Bar Class construct
          }
      }
      
    • Class Foo expect an instance of Class Bar as an argument of a method:

      import dir.barpackage.Bar;
      
      Class Foo
      {
          private int x;
      
          public Foo()
          {
              //Construct an Instance of the Foo object
          }
      
          public void doSomethingWithBar(Bar bar)
          {
              Foo.x = bar.getSomeBarPropertyValue();
          }
      }
      

    Going further on your question:

    For example, a method in the first class will be static, producing a Fibonacci number for instance, and another method in the second class will do something with the Fibonacci number which has been created by the method in first class

    The following example is one way of doing this:

    FirstClass.java

    Class FirstClass
    {
        private static int fibonnacciNumber; // This field is private to this class and thus can be only accessed by this class
    
        public static int getFibonnaciNumber() // A public method can be accessed any place other than your class
        {
            return FirstClass.fibonnacciNumber;
        }
    }
    

    SecondClass.java

    Class SecondClass
    {
        public void doSomethingWithFibonnacciNumber(int fibonnacciNumber)
        {
            //Will do something with your fibonnacci number;
        }
    }
    

    Example of usage

    SecondClass second = new SecondClass();
    second.doSomethingWithFibonnacciNumber(FirstClass.getFibonnacciNumber());
    

    I hope it helped. Cheers.

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

Sidebar

Related Questions

Im trying to create a server/client application that will work on two or more
In .NET4 MVC 4 using Code First I have two classes that form a
Is it possible to create installation package of two or more installation in visual
I want to create two widgets in flex. One for the movies ( that
I'm trying to create some classes that allow me to retrieve and manipulate a
Well, I need to create two questionnaires that are almost equals. The difference are
I have a number of classes that derive from a pure virtal base: class
If I have a base class and two derived classes, and I want to
I want to create a VS 2010 C# console application that connects to two
Summary Let's say I have two C# 4.0 classes, one inheriting from the other:

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.