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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:01:56+00:00 2026-05-31T14:01:56+00:00

So I have a method which instantiates a instance field String[][] myArray with values.

  • 0

So I have a method which instantiates a instance field String[][] myArray with values.

String[][] myArray;

public String[][] getArray() {
    // CREATES, RETURNS & INSTANTIATES myArray WITH ARRAY
}

However, then I have a method called getAvg() which gets the average of a column in the array. This uses the instance field which is now instantiated with data. Trouble is, this will obviously not run if the array has not been created first. Within my program, I create the array before calling for the average and it works. But I’m wondering if this is the most elegant of methods and if this could even be bad practice.

public double getAvg() {
    // CALCULATES AND RETURNS AVERAGE
}

I previously recreated the array within the getAvg() method. However, this lead me to me thinking that this would be redundant and inefficient.

So, what do you think?

  • 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-31T14:01:57+00:00Added an answer on May 31, 2026 at 2:01 pm

    Yes, this is bad practice. In general (there are exceptions, but not many), all the data a class needs should be ready by the time the constructor returns. What about renaming getArray to initializeMyArray, making it private, and placing a call to initializeMyArray in the constructor? Then, whenever some outside code calls getAvg(), myArray will have been initialized.

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

Sidebar

Related Questions

I have a method which returns an array of fixed type objects (let's say
I have a method which takes a directory path as a string. In the
I have got method which is using Assembly.LoadFrom(...) statement and returns the supported cultures
I have a method which returns some xml in a memory stream private MemoryStream
I have a method which compares two objects and returns a list of all
I have this method with a huge switch statement like this: public bool ExecuteCommand(string
I have a method which instantiates an object, some of the properties of this
Suppose I have a factory method, which wants to construct an instance of a
i have a method which takes in a DotNetOpenAuth (formally known as DotNetOpenId) Response
I have a method which I would like to invoke asynchronously: void Foo() {

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.