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

The Archive Base Latest Questions

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

i am using Java and i have this hierarchy class Seq class For extends

  • 0

i am using Java and i have this hierarchy

class Seq

class For extends Seq

interface SeqIt

Class ForIt implements SeqIt

And ForIt has a constructor: public ForIt( For x ) //takes For objects

Till now everything works fine.
Now I want to make a new class called FU that gets access to any ForIt objects I created.
And this FU class must have a method with this signature public static int sum1(For MyFor)
For example if ForIt objects have inside it an array called myArr. This method sum1 is supposed to return the sum of all elements of the array myArr inside MyFor object

How can I create this class FU?

  • 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-14T11:09:49+00:00Added an answer on June 14, 2026 at 11:09 am

    The 2 ways I can think of are:

    • Every time you create a ForIt class, add it to your FU class – when you are done, call the sum method
    • Have the ForIt class constructor call a static method of the FU class to let it know a new ForIt was created

    I would personally use option 1 as it avoids static variables which can be a pain to work with. Option 2 would work for a simple use case.

    //not thread safe
    class FU {
        private final List<ForIt> list = new ArrayList<> ();
        private int sum = 0;
    
        void add(ForIt fi) {
            list.add(fi);
            sum += fi.getValue();
        }
    
        int sum() {
            return sum;
        }
    }
    

    Option 1 is fairly straigthforward (create a FU object, and every time you create a ForIt instance, add it to the FU object).
    Option 2:

    class ForIt {
        private static final FU fu = new FU();
    
        ForIt() {
            fu.add(this);
        }
    
        static int sum() {
            return fu.sum();
        } 
    }
    
    public static void main(String... args) {
        new ForIt();
        new ForIt();
        System.out.println(ForIt.sum());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this username and password validation code using Java persistence. public Subscriber
I have the following Java object hierarchy: public interface Function { public void calculate(long
I have a class hierarchy similar to this one: public static class BaseConfiguration {
We have a class hierarchy similar to this one: public class TestDereference { private
I'm using Java 6. For simplicity, everything will be public. Suppose I have this
I have this issue: first, I execute a SQL query using Java and then
I have this Maven task to generate Java classes from an XSD file using
I'm using a Java wrapper for SQLite called SQLiteJDBC - this may have some
I am using iTextSharp in VB.net. I have this piece of code from java
I have certificate created using java class CertAndKeyGen and X500Name and I am able

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.