Author Topic: Para saan po ba ang Constructor sa Java? Saan sya ginagamit?  (Read 70 times)

0 Members and 1 Guest are viewing this topic.

Offline rpmolecule

  • Full Member
  • *
  • Posts: 231
  • Karma: +1/-0
  • Seize the Day!
    • View Profile
Para saan po ba ang Constructor sa Java? Saan sya ginagamit?

Techronnati | where technology never sleeps


Mountain View

Offline h2obubbler

  • Full Member
  • *
  • Posts: 228
  • Karma: +0/-0
  • Life is like a box of chocolate.
    • View Profile
Re: Para saan po ba ang Constructor sa Java? Saan sya ginagamit?
« Reply #1 on: July 21, 2016, 06:46:52 AM »
A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created. 


Here are the key differences between a constructor and a method:

-A constructor doesn’t have a return type.
-The name of the constructor must be the same as the name of the class.
-Unlike methods, constructors are not considered members of a class.

A constructor is called automatically when a new instance of an object is created.

Here’s the basic format for coding a constructor:

Code: [Select]
public ClassName (parameter-list) [throws exception...]
{
    statements...
}

Offline GBTalk

  • Full Member
  • *
  • Posts: 184
  • Karma: +0/-0
    • View Profile
Re: Para saan po ba ang Constructor sa Java? Saan sya ginagamit?
« Reply #2 on: July 21, 2016, 07:04:44 AM »
how about yung concept ng overload constructor? paano yun?

Offline MrSpecialist

  • Sr. Member
  • *
  • Posts: 345
  • Karma: +1/-0
  • I'm an expert!
    • View Profile
Re: Para saan po ba ang Constructor sa Java? Saan sya ginagamit?
« Reply #3 on: July 21, 2016, 07:05:28 AM »
Ah, bale ganto po yan. Supposed you're going to create a Java program na merong object na Pencil. Tapos, pag brand lang ang binigay, magkakaroon kagad ng default na grade ang Pencil na object. And default value ng grade ng Pencil ay 'HB'. Pero pag nagbigay ang user ng grade, ang grade na binigay ng user ang gagamitin:

Pencil.java:

Code: [Select]
package org.techronnati.myclasses;
public class Pencil {
private String brand_1;
private String grade_1;

public Pencil(String brand, String grade){
brand_1 = brand;
grade_1 = grade;
}

public Pencil(String brand){
brand_1 = brand;
grade_1 = "HB";
}

public String toString(){
return brand_1 + " "+ grade_1;
}

}

At ang MainClass.java (nilagyan ko nang line number sa gilid):

Code: [Select]
1 package org.techronnati.main;
2 import java.util.Scanner;
3 import org.techronnati.myclasses.Pencil;
4 public class MainClass {
5 public static void main(String[] args) {
6 Scanner scan = new Scanner(System.in);
7 String brand,
8 grade;
9 System.out.println("Input brand: ");
10 brand = scan.nextLine();
11 Pencil pencil = new Pencil(brand);
12 System.out.println(pencil);
13 System.out.println("Input another brand: ");
14 brand = scan.nextLine();
15 System.out.println("Input pencil's grade (HB, B, H, etc.): ");
16 grade=scan.nextLine();
17 Pencil pencil2 = new Pencil(brand, grade);
18 System.out.println(pencil2);
19 }
20 }


Kung titinangnan po natin yung line 11, nung nag instantiate po sya ng new Pencil, ang nilagay po nyang parameter ay 'brand' lang. So sa class po natin na Pencil, ang gagamitin na constructor ay ito kasi isang string lang ang parameter:

Code: [Select]
public Pencil(String brand){
setBrand_1(brand);
setGrade_1("HB");
}

Pero dun sa sa line 17, dalawa na ang argument na nilagay kaya ang gagamitin ng java na constructor ay ito:

Code: [Select]
public Pencil(String brand, String grade){
setBrand_1(brand);
setGrade_1(grade);
}

Isa pang tanong, pwede kaya na isa lang ang constructor? Tapos manually na lang ilagay yung grade na 'HB' pag brand lang ang binigay? Ang sagot, opo pwede din. Bale magiging ganto ang hitsura ng code natin:

MainClass2.java:

Code: [Select]
1 package org.techronnati.main;
2 import java.util.Scanner;
3 import org.techronnati.myclasses.Pencil2;
4 public class MainClass2 {
5 public static void main(String[] args) {
6 Scanner scan = new Scanner(System.in);
7 String brand,
8 grade;
9 System.out.println("Input brand: ");
10 brand = scan.nextLine();
11 Pencil2 pencil2 = new Pencil2(brand);
12 pencil2.setGrade_1("HB");
13 System.out.println(pencil2);
14 System.out.println("Input another brand: ");
15 brand = scan.nextLine();
16 System.out.println("Input pencil's grade (HB, B, H, etc.): ");
17 grade=scan.nextLine();
18 Pencil2 pencil2a = new Pencil2(grade);
19 pencil2a.setGrade_1("grade");
20 System.out.println(pencil2);
21 }
22 }

Pencil2.java:


Code: [Select]
package org.techronnati.myclasses;
public class Pencil2 {
String brand_1;
String grade_1;
public Pencil2(String brand){
brand_1 = brand;
}

public void setGrade_1(String grade){
grade_1 = grade;
}

public String toString(){
return brand_1 + " "+ grade_1;
}
}


Kaso ang pangit dito ay everytime na brand lang ang binigay, kelangan pa natin i-set manually sa code natin yung default value ng grade (line 12 ng MainClass2). Eh pano kung maraming beses sa codes natin na puro brand lang ang binigay, e di set tayo ng set ng default value. Bukod sa set tayo ng set, pag nag palit tayo ng default value, yung lahat ng mga set natin ng grade eh papaltan natin ng new value. Pero kung dun sa dalawang constructor ang gagamitin natin, pag nagpalit ng value yung default na grade, sa constructor lang tayo magpapalit.

Actually, pwede naman gamitin ang kahit ano sa kanilang dalawa. Mas convinient lang gamitin yung unang approach (yung dalawa ang constructor) sa program na ito kaysa sa pag isa lang ang constructor. Pero it doesn't mean na ang overloaded constructor ang pinaka magandang approach. Depende na yan sa diskarte ng programmer or designer kung anong approach ang gagamitin depende kung anong klaseng program ang gagawin. Ang mahalaga eh alam natin kung pano gumagana ang mga overloaded constructor (mga classes na more than 1 ang constructor kagaya nung Pencil.java).

Now, pwede bang 'Pencil pencil = new Pencil();' ang gamitin. Hindi po kasi wala namang constructor na walang parameter sa class na Pencil. Ang meron lang ay 'Pencil(String brand, String grade)' at 'Pencil(String brand)'. Unless maglagay po tayo ng pang tatlong constructor na 'Pencil()'. Pag naglagay po tayo ng constructor na yan, pwede na pong gamitin ang 'Pencil pencil = new Pencil();'.

Isa pa pong tanong, pano kung wala akong ginawang constructor. Kagaya nito:

Code: [Select]
package org.techronnati.myclasses;
public class Person {
private String name;
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Kung mapapansin, walang constructor tayo dito. Puro methods ('public String getName()' at 'public void setName(...)') at 'private String name;' lang ang laman ng class natin. Bakit pag ginamit natin dito:

Code: [Select]
package org.techronnati.main;
import org.techronnati.myclasses.Person;

public class MainClass3 {
public static void main(String[] args) {
Person human = new Person();
human.setName("mau dim jr");
System.out.println(human.getName());

}
}

ay wala syang error eh wala namang constructor yung Person na class. Ang sagot, this is because pag walang constructor na nilagay sa class natin, JVM will automatically create a default constructor na blank lang, based sa name ng class. Kaya sa 'Person human = new Person();' ay hindi sya nag error kahit wala namang 'Person()' na constructor yung Person na class.
« Last Edit: July 21, 2016, 07:07:52 AM by MrSpecialist »

 

Related Topics

  Subject / Started by Replies Last post
2 Replies
103 Views
Last post September 09, 2014, 09:06:36 AM
by MrSpecialist
0 Replies
45 Views
Last post October 24, 2014, 01:26:44 AM
by CyborgRepublic
13 Replies
180 Views
Last post August 17, 2017, 03:06:45 AM
by nomadic
1 Replies
59 Views
Last post July 21, 2016, 06:48:30 AM
by MrSpecialist
0 Replies
36 Views
Last post July 21, 2016, 07:14:50 AM
by CyborgRepublic

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.