#How does this array length work?

67 messages ยท Page 1 of 1 (latest)

soft wave
#

Somehow this code shows the following output: "The number of topics is 4". I am mainly confused with this code:

    
    System.out.println("The number of topics is "+ sampleFeed.getNumTopics());```

How is it able to know the length is 4?

```import java.util.Arrays;

public class Newsfeed {
  
  String[] topics = {"Opinion", "Tech", "Science", "Health"};
  int[] views = {0, 0, 0, 0};
  
  public Newsfeed(){

  }
    
  public String[] getTopics(){
    return topics;
  }
  
  public int getNumTopics(){
    return topics.length;
    
  }
  
  public static void main(String[] args){
    Newsfeed sampleFeed = new Newsfeed();
    
    System.out.println("The number of topics is "+ sampleFeed.getNumTopics());
   
  }
}
rich wharfBOT
#

โŒ› This post has been reserved for your question.

Hey @soft wave! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

forest musk
#

Well the method in question just returns the length of the array that holds the topics

soft wave
#

Yeah I think I see it now, the Newsfeed sampleFeed = new Newsfeed(); confused me, what is that doing exactly?

#

first it runs this in main()

forest musk
#

Well you create a new instance of the class Newsfeed

#

an object

soft wave
#

we need to do that?

forest musk
#

well you might want to have more than one Newsfeed in your program

soft wave
#

but for this output, not theoretically

#

is this Newsfeed sampleFeed = new Newsfeed(); required to achieve this output: "The number of topics is 4"

forest musk
#

If everything were static/ wouldn't run on the basis of objects we couldn't have different data

forest musk
#

you would need an instance of the class from which you could call that method

soft wave
#

oh I see

#

if getNumTopics was static we could just use this for the method call?
Newsfeed.getNumTopics()?

forest musk
#

yeah

soft wave
#

and it would work just the same?

forest musk
soft wave
#

I mean, the output would be the same

forest musk
#

because the method uses instance variables

#

it wouldn't work

soft wave
#

syntax error?

rich wharfBOT
soft wave
#

someone bought this domain for JavaBot?

#

I wonder if they live in Estonia for the .ee TLD

forest musk
#

Someone surely did. But not us no.

#

JavaBot is a creation of this discord.

soft wave
#

Made in Java?

forest musk
soft wave
soft wave
#

static String[] topics = {"Opinion", "Tech", "Science", "Health"};

forest musk
#

yeah ๐Ÿ™‚

soft wave
#

this way I can remove the object, HelloWorld sampleFeed = new HelloWorld();

#

and call it like this: System.out.println("The number of topics is "+ HelloWorld.getNumTopics());

forest musk
#

makes 0 sense but sure

soft wave
forest musk
#

well why would you get rid of the functionality? why would you want HelloWorldto be a completely static class?

soft wave
#

I'm not sure

#

I was wondering that myself

#

why would anyone wanna use static based?

#

if instance based is better

forest musk
#

its different not better

#

sometimes I would want a static method ๐Ÿ™‚

soft wave
#

oh yeah, I changed the class name because I tested in an existing HelloWorld.java file

soft wave
#

maybe not "better" per say

#

because I honestly don't know when to use instance or class based

#

main() appears to be class based, this is so that JVM runs it like this: ClassName.main() without instantiating a new object

#

so in terms of main() it must be static, otherwise JVM fails

forest musk
#

in 90% of the cases you would use an instance based approach

soft wave
#

Hmmm, OK

forest musk
#

here is a SO post that is quite good

soft wave
#

probably not too important, but just curious

forest musk
#

especially the second answer is for u I guess

soft wave
#

OK so depends on the field

#

it can be combination of instance and static based, I believe

#

depends on the fields and what works best for them

#

in the case of main() it works best with class based

#

maybe it's less memory usage, no new object is required, not sure

#

or just easier to use in that case, otherwise we would be doing this every time which is even more code:
"ClassName main = new ClassName()"