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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:26:32+00:00 2026-05-17T06:26:32+00:00

Simple question. A friend of mind wrote code similar to this one (which is

  • 0

Simple question. A friend of mind wrote code similar to this one (which is just to explain you my question, it’s not useful at all….)

class Example{
    private int[] tab = new int[10];
    public Example() {
        for(int i = 0 ; i < 10 ; i++)
            tab[i] = (int)(Math.random()*100);
        for(int i = 0 ; i < 10 ; i++)
            System.out.println(tab[i]);
    }
    public static void main(String[] arg) {
        Example ex = new Example();
    }
}

I told him he should put the new inside the constructor

class Example{
    private int[] tab;
    public Example() {
        tab = new int[10];
    ...
}

When he ask me why, I din’t know what to answer : I didn’t have a definite argument other than “it’s better this way”. The way I learn it, you can initialize variables with basic types (int, double…) but for arrays you should do it in the constructor.

So:

  • is it really better?
  • Are there some good reasons: Convention, style?
  • Does it change anything like less/more memory used?

I’m not considering the case where the number of element can vary. It will ALWAYS be 10

  • 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-17T06:26:32+00:00Added an answer on May 17, 2026 at 6:26 am
    • is it really better ?

    Not really, IMO.

    • is there some good reasons : convention ? style ?

    There may be valid reasons for choosing one way over the other is when you have multiple constructors, or when the initial values depend on constructor arguments; e.g.

    private int[] tab;
    
    public Example(int size) {
        tab = new int[size];
        for (int i = 0; i < size; i++)
            tab[i] = (int) (Math.random() * 100);
    }
    

    or

    private int[] tab = new int[10];
    
    public Example(int initial) {
        for (int i = 0; i < 10; i++)
            tab[i] = initial;
    }
    
    public Example() {
        for (int i = 0; i < 10; i++)
            tab[i] = (int) (Math.random() * 100);
    }
    

    Apart from that (and in your example) there are no general rules about this. It’s a matter of personal taste.

    • does it change anything like less/more memory used ?

    In your example it will make no difference. In general, there might be a tiny difference in the code size or performance, but it is not worth worrying about.

    In short, I don’t think your suggestion to your friend has a rational basis.

    The way I learn it, you can initialize variables with basic types (int, double…) but for arrays you should do it in the constructor.

    You should unlearn that … or at least recognize that it is just a personal preference.

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

Sidebar

Related Questions

Simple question: How do I do this on one line: my $foo = $bar->{baz};
This is probably a really simple question however Google isn't my friend today. I
One of my friend was asked this question in an interview - You have
My friend sent me a link to this little page which shows a simple
Simple question, but one that I've been curious about...is there a functional difference between
Simple question: Can a swing frame be completely modal ( block all others windows
Simple question, hopefully an easy way and just want to verify I'm doing it
Simple question: Can I mix in my desktop application Java and JavaFX Script code?
Apologies for the rather simple question, I just can't seem to find ANY good
I'm not sure if this is an appropriate question for StackOverflow. I have a

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.