Jpaji Rajnish
Jpaji Rajnish

Reputation: 1501

How does Java allocate memory for objects?

I've been reading through some android tutorials and I saw this:

public class HelloService extends Service {
  private Looper mServiceLooper;
  private ServiceHandler mServiceHandler;
  ...

I assume when you declare an integer like public int num1; the compiler allocates integer-size memory for it right when it sees it. But how could it do something like this for all objects? Are all objects allocated the same memory and being more specific is just for type-safety/readability purposes?

Upvotes: 2

Views: 464

Answers (1)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

mServiceLooper and mServiceHandler fields are simply pointers to instances of their corresponding classes. All pointers occupy the same space, e.g. 4 bytes, it depends on JVM.

Upvotes: 1

Related Questions