• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt On Memory Allocation

 
Jagan Mohan Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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..
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • 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
 
Clinton Edwards
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Mark,
So, what is the difference? The info would be appreciated.
Clinton
 
Morten Moeller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!