This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

initialization of variables

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can the instance and static variables be initialized at definition time.the confusion is ,since the class is only a structure(template)and doesn't take space in memory .so where does it stores the data.

like:

class ex{
int a=10;
..
}
------------------
be yourself
 
Trailboss
Posts: 23418
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each time you create a new class, memeory is set aside on the heap to store the attributes (instance variables) of the new object.
Right after the memory is allocated, all of the attributes are initialized to something. Then the constructor is called.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand this further, I imagine that the class variables are stored with the loaded Class instance, where as the instance variables are stored with the instances of the class on the heap as Paul described.
Class variables, those with the static keyword can be initialized at declaration time or within the static {} block. The static block is a special block of code that is executed, if it exists, after a class has been loaded and before the first instance is constructed. Thus, a class variable x can be initialized with private static int x = 5; or via the following:
class SomeClass
{
private static int x;
static
{
// This block gets executed after
// the class has been loaded but
// before any instances have been
// created.
x = 5;
}
}
Instance variables can be initialized in one of three places: at declaration, within the instance initialization block, or within a constructor itself. At declaration, this means private int x = 5;. The instance initialization block is another special block of code that is executed, again if it exists, as part of the construction of an instance of a class. I cannot remember if the instance initialization block is executed before or during the constructor being called, but it can be done as follows:
class SomeClass
{
private int x;
{
// instance initialization block.
x = 5;
}
}
Then of course, the initialization within a constructor is very straightforward.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic