• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple questions! (String and static)  RSS feed

 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.Let's start with String. Isn't it immutable? Meaning it's value cannot be changed once it is initialized,but the code below compiles and executes just fine:

Doesn't this mean that String is mutable?
2. A static variable is connected to a class rather than an instance of that class. So static members cannot be accessed from non-static context. But the code below compiles and executes:

A constructor is not a static context. How can the static variable str be accessed from the constructor?
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand the question I asked about String. "a" is just a reference. So 'a' can be made to refer to different String objects but the String objects themselves cannot be changed. Am I right?
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are.
And you can check this link http://www.journaldev.com/797/what-is-java-string-pool so you'll better understand difference of creating Strings.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about my second question? I'm still stuck with it!
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realized how stupid my questions are!!
One of the main uses of the static member variable is to keep track of the number of instances of the class. If the static member is not accessible through the constructor then it's purpose cannot be fulfilled.
Sorry if I caused any inconvenience!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:One of the main uses of the static member variable is to keep track of the number of instances of the class.

Not really. In fact, I've never used a static variable in this way. Any variable (or method) that does not depend on a particular instance of the class can be static.

IMHO the most common use of static variables is for static final constants.

So static members cannot be accessed from non-static context.

No, it's the other way round.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:One of the main uses of the static member variable is to keep track of the number of instances of the class.

I think that is one of the main EXAMPLES used to illustrate a static member variable, but outside of programming lectures and books, I've never seen it used this way.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your help! Much appreciated!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!