• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt On Memory Allocation

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anybody help me in telling me the difference between the memory allocation of these 2 scenarios
There are 2 classes A and B as foolows
class B {
int x = 5;
}
The class A can be in one form as
1. class A extends B {
int y= 7;
}
2. class A {
int y = 5;
public A() {
B b = new B();
}
}

Any suggestions in this regard are appreciated.
Regards,
Jagan Mohan Reddy..
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this doesn't have anything to do with Swing, JFC, or AWT, I'm moving it to another, more appropriate forum... how about the Performance forum?

-Nate
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's not so much an issue of memory allocation so much as object relations. The latter does effect the former, but is never (at least not in any designs I've ever seen or heard of) strong enough motivation to drive the former.
In the first case, A is a subclass of B. In the second case, A has an instance of B contained within it. Are you familiar with the difference between these concepts?
--Mark
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello Mark,
So, what is the difference? The info would be appreciated.
Clinton
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in the first A extends B, so methods and and variables exposed in B will be available in A.
So (if you had made variables public or from within the same package) A.x is valid and returns 5 in the first example, but not valid in the second.
The other thing is that B b is a local variable in the constructor, so even if you get the new instance of B, it will be released the moment the constructor is done. In order to keep B around, you would have to make B a global variable in the class.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic