top of page

Constructor and memory - JAVA

Hello people! Today I will talk about how to create object in JAVA. So we have few options:

- default constructor (available when no other exist),

- constructor without parameters,

- constructor with parameters,

- copy constructor (as argument parameter from your class).

In constructors we can prepare objects. As example we can use cake class.

cake() {};

cake(string name) { cake_name = name };

cake(cake Cake) { //make copy of object };

So without constructor you can make:

cake Cake = new cake();

With constructors you can write: cake Cake = new cake(); - no parameters

cake Cake2 = new cake("Brownie"); - 1 parameter function

cake Cake3(Cake2); - copy constructor (copy "Brownie cake")

Destructors: When object's life is going to end we need to clean memory, but we have "trash" governor which is cleaning after us. How exactly it works? It depend from version/generation, but it is not this topic. Sometimes we need to do some extra things, before objects will be destroyed, but we can talk about it later. Bye!

Subscribe me!

Be sure that you read all.

  • Facebook Clean
  • Black Twitter Icon
  • Black Instagram Icon
  • Black YouTube Icon
Recent Posts
bottom of page