Skip to content · ⁨Lompat ke konten⁩

Class Creation · ⁨Pembuatan Kelas⁩

AP Computer Science A · ⁨AP Ilmu Komputer A⁩ · Topic 3 · ⁨Topik 3⁩

Video lesson for this topic · ⁨Pelajaran video untuk topik ini⁩ Open the video page · ⁨Buka halaman video⁩
7:26

Pembuatan Kelas

Berikut seorang siswa, disimpan sebagai tiga variabel terpisah: nama, tahun, dan nilai. Itu berhasil. Sekarang tambahkan seribu siswa. Anda memerlukan tiga ribu variabel…

English narration · English + 中文 subtitles burned in · ⁨Narasi bahasa Inggris · Subtitle bahasa Inggris + 中文 disematkan langsung⁩

3.1

Abstraction and Program Design · ⁨Abstraksi dan Desain Program⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.1.A: Represent the design of a program by using natural language or creating diagrams that indicate the classes in the program and the data and procedural abstractions found in each class by including all attributes and behaviors.

  • 3.1.A.1 Abstraction is the process of reducing complexity by focusing on the main idea. By hiding details irrelevant to the question at hand and bringing together related and useful details, abstraction reduces complexity and allows one to focus on the idea.
  • 3.1.A.2 Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation. Data abstraction manages complexity by giving data a name without referencing the specific details of the representation. Data can take the form of a single variable or a collection of data, such as in a class or a set of data.
  • 3.1.A.3 An attribute is a type of data abstraction that is defined in a class outside any method or constructor. An instance variable is an attribute whose value is unique to each instance of the class. A class variable is an attribute shared by all instances of the class.
  • 3.1.A.4 Procedural abstraction provides a name for a process and allows a method to be used only knowing what it does, not how it does it. Through method decomposition, a programmer breaks down larger behaviors of the class into smaller behaviors by creating methods to represent each individual smaller behavior. A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for code reuse, which helps manage complexity.
  • 3.1.A.5 Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.
  • 3.1.A.6 Using procedural abstraction in a program allows programmers to change the internals of a method (to make it faster, more efficient, use less storage, etc.) without needing to notify method users of the change as long as the method signature and what the method does is preserved.
  • 3.1.A.7 Prior to implementing a class, it is helpful to take time to design each class including its attributes and behaviors. This design can be represented using natural language or diagrams.
Bahasa Indonesia

Tujuan Pembelajaran 3.1.A: Representasikan desain program menggunakan bahasa alami atau membuat diagram yang menunjukkan kelas-kelas dalam program dan abstraksi data serta prosedural yang ditemukan di setiap kelas dengan menyertakan semua atribut dan perilaku.

  • 3.1.A.1 Abstraksi adalah proses mengurangi kompleksitas dengan memfokuskan pada ide utama. Dengan menyembunyikan detail yang tidak relevan dengan pertanyaan saat ini dan mengumpulkan detail yang terkait dan berguna, abstraksi mengurangi kompleksitas dan memungkinkan seseorang untuk memfokuskan pada ide tersebut.
  • 3.1.A.2 Abstraksi data menyediakan pemisahan antara sifat abstrak dari tipe data dan detail konkret dari representasinya. Abstraksi data mengelola kompleksitas dengan memberikan nama pada data tanpa merujuk pada detail spesifik representasinya. Data dapat berbentuk variabel tunggal atau kumpulan data, seperti dalam kelas atau set data.
  • 3.1.A.3 Atribut adalah jenis abstraksi data yang didefinisikan dalam kelas di luar metode atau konstruktor apa pun. Variabel instance adalah atribut yang nilainya unik untuk setiap instance dari kelas tersebut. Variabel kelas adalah atribut yang dibagikan oleh semua instance dari kelas tersebut.
  • 3.1.A.4 Abstraksi prosedural menyediakan nama untuk suatu proses dan memungkinkan sebuah metode digunakan hanya dengan mengetahui apa yang dilakukannya, bukan bagaimana hal itu dilakukan. Melalui dekomposisi metode, seorang programmer memecah perilaku kelas yang lebih besar menjadi perilaku yang lebih kecil dengan membuat metode untuk mewakili setiap perilaku kecil individu. Abstraksi prosedural dapat mengekstrak fitur bersama untuk mengeneralisasi fungsi daripada menduplikasi kode. Hal ini memungkinkan penggunaan kembali kode, yang membantu mengelola kompleksitas.
  • 3.1.A.5 Menggunakan parameter memungkinkan prosedur digeneralisasikan, memungkinkan prosedur digunakan kembali dengan rentang nilai input atau argumen.
  • 3.1.A.6 Menggunakan abstraksi prosedural dalam program memungkinkan programmer mengubah internal metode (untuk membuatnya lebih cepat, lebih efisien, menggunakan penyimpanan kurang, dll.) tanpa perlu memberi tahu pengguna metode tentang perubahan tersebut selama tanda tangan metode dan apa yang dilakukan metode tetap terjaga.
  • 3.1.A.7 Sebelum mengimplementasikan kelas, sangat membantu meluangkan waktu untuk merancang setiap kelas termasuk atribut dan perilakunya. Desain ini dapat direpresentasikan menggunakan bahasa alami atau diagram.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

Abstraction 抽象 means hiding detail behind a simple interface – you use a String without knowing how it stores characters. Good design breaks a problem into classes, each responsible for one idea. This topic is about writing your own classes.

Bahasa Indonesia
Teka-teki yang sedang berlangsung: kelas dan metode adalah bagian modular dari desain program yang lebih besar
Teka-teki yang sedang berlangsung: kelas dan metode adalah bagian modular dari desain program yang lebih besar

Abstraksi berarti menyembunyikan detail di balik antarmuka sederhana – Anda menggunakan String tanpa tahu bagaimana cara menyimpan karakter. Desain yang baik memecah masalah menjadi kelas-kelas, masing-masing bertanggung jawab atas satu ide. Topik ini tentang menulis kelas Anda sendiri.

Mendekomposisi program menjadi modul dan sub-modul
Mendekomposisikan program menjadi modul dan sub-modul
3.2

The Impact of Program Design · ⁨Dampak Desain Program⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.2.A: Explain the social and ethical implications of computing systems.

  • 3.2.A.1 System reliability refers to the program being able to perform its tasks as expected under stated conditions without failure. Programmers should make an effort to maximize system reliability by testing the program with a variety of conditions.
  • 3.2.A.2 The creation of programs has impacts on society, the economy, and culture. These impacts can be both beneficial and harmful. Programs meant to fill a need or solve a problem can have unintended harmful effects beyond their intended use.
  • 3.2.A.3 Legal issues and intellectual property concerns arise when creating programs. Programmers often reuse code written by others and published as open source and free to use. Incorporation of code that is not published as open source requires the programmer to obtain permission and often purchase the code before integrating it into their program.
Bahasa Indonesia

Tujuan Pembelajaran 3.2.A: Jelaskan implikasi sosial dan etika dari sistem komputasi.

  • 3.2.A.1 Keandalan sistem merujuk pada kemampuan program untuk menjalankan tugasnya sesuai harapan di bawah kondisi yang ditetapkan tanpa kegagalan. Pemrogram harus berusaha memaksimalkan keandalan sistem dengan menguji program dengan berbagai kondisi.
  • 3.2.A.2 Pembuatan program memiliki dampak pada masyarakat, ekonomi, dan budaya. Dampak-dampak ini dapat bersifat menguntungkan maupun merugikan. Program yang dirancang untuk memenuhi kebutuhan atau memecahkan masalah dapat menimbulkan efek merugikan tak terduga di luar penggunaannya yang dimaksudkan.
  • 3.2.A.3 Isu hukum dan kekhawatiran terkait hak kekayaan intelektual muncul saat pembuatan program. Para pemrogram sering kali menggunakan ulang kode yang ditulis oleh orang lain dan dipublikasikan sebagai sumber terbuka (open source) serta gratis digunakan. Penggunaan kode yang tidak dipublikasikan sebagai sumber terbuka mengharuskan pemrogram untuk mendapatkan izin dan seringkali membeli kode tersebut sebelum mengintegrasikannya ke dalam program mereka.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

Design choices affect whether code is correct, readable, and reusable. Encapsulation 封装 – keeping data private and exposing it only through methods – protects an object's state from misuse and lets you change the inside without breaking users of the class. Thoughtful naming, single-purpose methods, and testing reduce bugs.

Design also carries responsibility beyond the code. System reliability 系统可靠性 - a program performing its tasks as expected, without failure - is something programmers should maximise through careful design and testing. Programs have real impacts on society, the economy, and culture that can be both beneficial and harmful. And creating programs raises legal and intellectual-property 知识产权 concerns: programmers often reuse code published as open source 开源 and free to use, but must respect its licence and give credit rather than copy others' work as their own.

Bahasa Indonesia

Pilihan desain mempengaruhi apakah kode benar, mudah dibaca, dan dapat digunakan kembali. Enkapsulasi – menyimpan data secara privat dan mengeksposnya hanya melalui metode – melindungi state objek dari penyalahgunaan dan memungkinkan Anda mengubah bagian dalam tanpa merusak pengguna kelas. Penamaan yang bijaksana, metode tunggal-tujuan, dan pengujian mengurangi bug.

Desain juga membawa tanggung jawab di luar kode. Keandalan sistem - sebuah program yang menjalankan tugasnya sesuai harapan, tanpa kegagalan - adalah sesuatu yang pemrogram harus maksimalkan melalui desain dan pengujian yang hati-hati. Program memiliki dampak nyata pada masyarakat, ekonomi, dan budaya yang dapat baik menguntungkan maupun merugikan. Dan membuat program menimbulkan kekhawatiran hukum dan hak kekayaan intelektual: pemrogram sering menggunakan ulang kode yang dipublikasikan sebagai open source dan gratis digunakan, tetapi harus menghormati lisensinya dan memberikan kredit daripada meniru karya orang lain sebagai milik mereka sendiri.

Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
Abstraction/əbˈstrækʃn/ Abstraksi
Encapsulation/ɪnˌkæpsjʊˈleɪʃn/ Pencapsulan (Encapsulation)
System reliability/ˈsɪstəm rɪˌlaɪəˈbɪlɪti/ Keandalan sistem
legal and intellectual-property/ˈliːɡl ænd ˌɪntəˈlektʃuːəl ˈprɒpəti/ hukum dan properti intelektual
open source/ˈəʊpən sɔːs/ open source
instance variables/ˈɪnstəns ˈveərɪəblz/ variabel instance
3.3

The Anatomy of a Class · ⁨Anatomi Kelas⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.3.A: Develop code to designate access and visibility constraints to classes, data, constructors, and methods.

  • 3.3.A.1 Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes. The keywords public and private affect the access of classes, data, constructors, and methods. The keyword private restricts access to the declaring class, while the keyword public allows access from classes outside the declaring class.
  • 3.3.A.2 In this course, classes are always designated public and are declared with the keyword class.
  • 3.3.A.3 In this course, constructors are always designated public.
  • 3.3.A.4 Instance variables belong to the object, and each object has its own copy of the variable.
  • 3.3.A.5 Access to attributes should be kept internal to the class in order to accomplish encapsulation. Therefore, it is good programming practice to designate the instance variables for these attributes as private unless the class specification states otherwise.
  • 3.3.A.6 Access to behaviors can be internal or external to the class. Methods designated as public can be accessed internally or externally to a class, whereas methods designated as private can only be accessed internally to the class.
Bahasa Indonesia

Tujuan Pembelajaran 3.3.A: Mengembangkan kode untuk menetapkan batasan akses dan visibilitas pada kelas, data, konstruktor, dan metode.

  • 3.3.A.1 Encapsulasi data adalah teknik di mana detail implementasi sebuah kelas disembunyikan dari kelas eksternal. Kata kunci public dan private memengaruhi akses terhadap kelas, data, konstruktor, dan metode. Kata kunci private membatasi akses hanya pada kelas pengumuman, sedangkan kata kunci public memungkinkan akses dari kelas di luar kelas pengumuman.
  • 3.3.A.2 Dalam kursus ini, kelas selalu ditetapkan sebagai public dan dideklarasikan dengan kata kunci class.
  • 3.3.A.3 Dalam kursus ini, konstruktor selalu ditetapkan sebagai public.
  • 3.3.A.4 Variabel instance milik objek, dan setiap objek memiliki salinan variabelnya sendiri.
  • 3.3.A.5 Akses terhadap atribut harus dijaga tetap internal dalam kelas guna mencapai encapsulasi. Oleh karena itu, praktik pemrograman yang baik adalah menetapkan variabel instance untuk atribut-atribut tersebut sebagai private kecuali spesifikasi kelas menyatakan sebaliknya.
  • 3.3.A.6 Akses terhadap perilaku bisa bersifat internal atau eksternal terhadap kelas. Metode yang ditetapkan sebagai public dapat diakses secara internal maupun eksternal terhadap sebuah kelas, sementara metode yang ditetapkan sebagai private hanya dapat diakses secara internal terhadap kelas.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

A class has three parts: instance variables 实例变量 (fields – the object's data), constructors (build objects), and methods (behavior). Fields are usually private; methods are usually public:

Bahasa Indonesia

Sebuah kelas memiliki tiga bagian: variabel instance (field – data objek), konstruktor (membangun objek), dan metode (perilaku). Field biasanya private; metode biasanya public:

Diagram kelas: atribut privat dan publik metode
Diagram kelas: atribut privat dan publik metode
public class Student {
    private String name;      // instance variable
    private int score;

    public Student(String n, int s) {   // constructor
        name = n;
        score = s;
    }
    public int getScore() { return score; }   // accessor
}
Cetak biru: sebuah kelas adalah template yang mendefinisikan bagaimana objek tipe tersebut dibangun
Cetak biru: sebuah kelas adalah template yang mendefinisikan bagaimana objek tipe tersebut dibangun
Explore · ⁨Jelajahi⁩

See an object's fields as boxes · ⁨Lihat bidang objek sebagai kotak-kotak⁩

A class groups related data (its fields) and methods. Each object gets its own set of field boxes; assigning to one changes that object only. · ⁨Kelas mengelompokkan data terkait (bidangnya) dan metode. Setiap objek mendapatkan set kotak bidangnya sendiri; menugaskan ke salah satunya hanya mengubah objek tersebut.⁩

Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
constructor/kənˈstrʌktə/ konstruktor
accessor (getter)/əkˈsesə/ aksesor (getter)
3.4

Constructors · ⁨Konstruktor⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.4.A: Develop code to declare instance variables for the attributes to be initialized in the body of the constructors of a class.

  • 3.4.A.1 An object's state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. This defines a has-a relationship between the object and its instance variables.
  • 3.4.A.2 A constructor is used to set the initial state of an object, which should include initial values for all instance variables. When a constructor is called, memory is allocated for the object and the associated object reference is returned. Constructor parameters, if specified, provide data to initialize instance variables.
  • 3.4.A.3 When a mutable object is a constructor parameter, the instance variable should be initialized with a copy of the referenced object. In this way, the instance variable does not hold a reference to the original object, and methods are prevented from modifying the state of the original object.
  • 3.4.A.4 When no constructor is written, Java provides a no-parameter constructor, and the instance variables are set to default values according to the data type of the attribute. This constructor is called the default constructor.
  • 3.4.A.5 The default value for an attribute of type int is 0. The default value of an attribute of type double is 0.0. The default value of an attribute of type boolean is false. The default value of a reference type is null.
Bahasa Indonesia

Tujuan Pembelajaran 3.4.A: Mengembangkan kode untuk mendeklarasikan variabel instance bagi atribut yang akan diinisialisasi dalam tubuh konstruktor sebuah kelas.

  • 3.4.A.1 Status (state) suatu objek merujuk pada atribut dan nilainya pada waktu tertentu, dan didefinisikan oleh variabel instance yang dimiliki objek tersebut. Hal ini mendefinisikan hubungan memiliki (has-a) antara objek dan variabel instance-nya.
  • 3.4.A.2 Konstruktor digunakan untuk menetapkan status awal sebuah objek, yang harus mencakup nilai awal untuk semua variabel instance. Ketika konstruktor dipanggil, memori dialokasikan untuk objek dan referensi objek yang terkait dikembalikan. Parameter konstruktor, jika ditentukan, menyediakan data untuk menginisialisasi variabel instance.
  • 3.4.A.3 Ketika objek yang dapat diubah (mutable object) merupakan parameter konstruktor, variabel instance harus diinisialisasi dengan salinan objek yang dirujuk. Dengan cara ini, variabel instance tidak memegang referensi ke objek asli, dan metode dicegah dari memodifikasi status objek asli.
  • 3.4.A.4 Ketika tidak ada konstruktor yang ditulis, Java menyediakan konstruktor tanpa parameter, dan variabel instance diatur ke nilai default sesuai dengan tipe data atributnya. Konstruktor ini disebut konstruktor default.
  • 3.4.A.5 Nilai default untuk atribut bertipe int adalah 0. Nilai default untuk atribut bertipe double adalah 0.0. Nilai default untuk atribut bertipe boolean adalah false. Nilai default untuk tipe referensi adalah null.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

A constructor 构造函数 has the same name as the class and no return type. It runs when you write new, and its job is to initialize the fields. A class can have several constructors with different parameter lists (overloading 重载); a no-argument constructor sets defaults.

Bahasa Indonesia

Sebuah konstruktor memiliki nama yang sama dengan kelas dan tidak memiliki tipe kembalian. Ia berjalan ketika Anda menulis new, dan tugasnya adalah menginisialisasi field. Sebuah kelas dapat memiliki beberapa konstruktor dengan daftar parameter yang berbeda (overloading); sebuah konstruktor tanpa-argumen menetapkan nilai default.

Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
overloading/ˌəʊvəˈləʊdɪŋ/ pemuatan ulang
3.5

Methods: How to Write Them · ⁨Metode: Cara Menulisnya⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.5.A: Develop code to define behaviors of an object through methods written in a class using primitive values and determine the result of calling these methods.

  • 3.5.A.1 A void method does not return a value. Its header contains the keyword void before the method name.
  • 3.5.A.2 A non-void method returns a single value. Its header includes the return type in place of the keyword void.
  • 3.5.A.3 In non-void methods, a return expression compatible with the return type is evaluated, and the value is returned. This is referred to as return by value.
  • 3.5.A.4 The return keyword is used to return the flow of control to the point where the method or constructor was called. Any code that is sequentially after a return statement will never be executed. Executing a return statement inside a selection or iteration statement will halt the statement and exit the method or constructor.
  • 3.5.A.5 An accessor method allows objects of other classes to obtain a copy of the value of instance variables or class variables. An accessor method is a non-void method.
  • 3.5.A.6 A mutator (modifier) method is a method that changes the values of the instance variables or class variables. A mutator method is often a void method.
  • 3.5.A.7 Methods with parameters receive values through those parameters and use those values in accomplishing the method's task.
  • 3.5.A.8 When an argument is a primitive value, the parameter is initialized with a copy of that value. Changes to the parameter have no effect on the corresponding argument.
Bahasa Indonesia

Tujuan Pembelajaran 3.5.A: Mengembangkan kode untuk mendefinisikan perilaku objek melalui metode yang ditulis dalam kelas menggunakan nilai primitif dan menentukan hasil pemanggilan metode-metode tersebut.

  • 3.5.A.1 Metode void tidak mengembalikan nilai. Header-nya berisi kata kunci void sebelum nama metode.
  • 3.5.A.2 Metode non-void mengembalikan satu nilai. Header-nya mencakup tipe kembali (return type) menggantikan kata kunci void.
  • 3.5.A.3 Dalam metode non-void, ekspresi return yang kompatibel dengan tipe kembali dievaluasi, dan nilainya dikembalikan. Ini disebut kembali melalui nilai (return by value).
  • 3.5.A.4 Kata kunci return digunakan untuk mengembalikan alur kendali ke titik di mana metode atau konstruktor dipanggil. Kode apa pun yang berada secara berurutan setelah pernyataan return tidak akan pernah dieksekusi. Mengeksekusi pernyataan return di dalam pernyataan pemilihan atau iterasi akan menghentikan pernyataan tersebut dan keluar dari metode atau konstruktor.
  • 3.5.A.5 Metode aksesor memungkinkan objek dari kelas lain untuk mendapatkan salinan nilai variabel instance atau variabel kelas. Metode aksesor adalah metode non-void.
  • 3.5.A.6 Metode mutator (modifikator) adalah metode yang mengubah nilai variabel instance atau variabel kelas. Metode mutator sering kali merupakan metode void.
  • 3.5.A.7 Metode dengan parameter menerima nilai melalui parameter tersebut dan menggunakan nilai-nilai tersebut dalam menyelesaikan tugas metode.
  • 3.5.A.8 Ketika argumen adalah nilai primitif, parameter diinisialisasi dengan salinan nilai tersebut. Perubahan pada parameter tidak berpengaruh pada argumen yang bersangkutan.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

A method has a signature, a return type, and a body. An accessor (getter) 访问器 returns information without changing the object; a mutator (setter) 修改器 changes a field. A method returning a value must have a return of the right type on every path; a void method returns nothing.

Bahasa Indonesia

Sebuah metode memiliki signature, tipe kembalian, dan badan. Sebuah aksesor (getter) mengembalikan informasi tanpa mengubah objek; sebuah mutator (setter) mengubah field. Metode yang mengembalikan nilai harus memiliki return dari tipe yang benar di setiap jalur; metode void tidak mengembalikan apa pun.

public void setScore(int s) { score = s; }   // mutator
public String toString() { return name + ": " + score; }
Explore · ⁨Jelajahi⁩

Follow a method call and its return · ⁨Ikuti pemanggilan metode dan pengembaliannya⁩

Calling a method pushes a frame with its parameters; when it hits return, the frame pops and the value goes back to the caller. · ⁨Memanggil metode mendorong bingkai dengan parameternya; ketika mencapai return, bingkai dilepas dan nilai kembali ke Caller.⁩

Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
mutator (setter)/mjuːˈteɪtə/ mutator (setter)
static (class) variable/ˈstætɪk ˈveərɪəbl/ variabel statis (kelas)
3.6

Passing and Returning References of an Object · ⁨Mengirim dan Mengembalikan Referensi Objek⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.6.A: Develop code to define behaviors of an object through methods written in a class using object references and determine the result of calling these methods.

  • 3.6.A.1 When an argument is an object reference, the parameter is initialized with a copy of that reference; it does not create a new independent copy of the object. If the parameter refers to a mutable object, the method or constructor can use this reference to alter the state of the object. It is good programming practice to not modify mutable objects that are passed as parameters unless required in the specification.
  • 3.6.A.2 When the return expression evaluates to an object reference, the reference is returned, not a reference to a new copy of the object.
  • 3.6.A.3 Methods cannot access the private data and methods of a parameter that holds a reference to an object unless the parameter is the same type as the method's enclosing class.
Bahasa Indonesia

Tujuan Pembelajaran 3.6.A: Mengembangkan kode untuk mendefinisikan perilaku objek melalui metode yang ditulis dalam kelas menggunakan referensi objek dan menentukan hasil pemanggilan metode-metode tersebut.

  • 3.6.A.1 Ketika argumen adalah referensi objek, parameter diinisialisasi dengan salinan dari referensi tersebut; hal ini tidak membuat salinan objek baru yang independen. Jika parameter merujuk pada objek yang dapat diubah (mutable), metode atau konstruktor dapat menggunakan referensi ini untuk mengubah keadaan objek. Merupakan praktik pemrograman yang baik untuk tidak memodifikasi objek yang dapat diubah yang dilempar sebagai parameter kecuali diperlukan dalam spesifikasi.
  • 3.6.A.2 Ketika ekspresi return mengevaluasi ke sebuah referensi objek, maka referensilah yang dikembalikan, bukan referensi ke salinan objek baru.
  • 3.6.A.3 Metode tidak dapat mengakses data pribadi dan metode dari parameter yang memegang referensi ke objek kecuali jika parameter tersebut memiliki tipe yang sama dengan kelas pembungkus dari metode tersebut.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English
= copies the reference, not the object

When you pass an object to a method, Java copies the reference, so the method acts on the same object – changes to its fields are visible to the caller. (Primitives are copied by value, so changes to them are not.) A method can also return a reference to an object. Because a String is immutable, passing one is safe; passing a mutable object lets a method change it.

Exam skill: know that mutating an object's fields inside a method affects the original, but reassigning the parameter (param = new...) does not affect the caller.

Worked example. Suppose s is a Student with score 50, and we call tweak(s):

Line (1) changes the object s points to, so the caller now sees 100. Line (2) makes the method's own copy of the reference point at a fresh object – the caller's s is untouched – and line (3) affects only that new object. After the call, s.getScore() is 100: the mutation stuck, the reassignment did not.

Bahasa Indonesia
= menyalin referensi, bukan objek

Ketika Anda mengirim objek ke metode, Java menyalin referensi, sehingga metode bertindak pada objek yang sama – perubahan pada field-nya terlihat oleh pemanggil. (Primitif disalin oleh nilai, jadi perubahannya tidak.) Metode juga dapat mengembalikan referensi ke sebuah objek. Karena String bersifat immutable, mengirimkannya aman; mengirim objek yang dapat diubah memungkinkan metode mengubahnya.

Java mengirim by value: metode mendapatkan salinan; true pass by reference, yang tidak dimiliki Java, akan memungkinkannya assigns ulang variabel pemanggil
Java selalu mengirim by value (kiri): metode mendapatkan salinan dari referensinya. True pass by reference (kanan) – yang tidak dimiliki Java – akan memungkinkannya metode assigns ulang variabel pemanggil itu sendiri.

Keterampilan ujian: ketahui bahwa memutasi field objek di dalam metode mempengaruhi yang asli, tetapi assigns ulang parameter (param = new...) tidak mempengaruhi pemanggil.

Contoh terpecahkan. Misalkan s adalah Student dengan skor 50, dan kami memanggil tweak(s):

public static void tweak(Student a) {
    a.setScore(100);          // (1) mutates the shared object
    a = new Student("Z", 0);  // (2) repoints the local copy only
    a.setScore(5);            // (3) changes only the new local object
}

Baris (1) mengubah objek yang ditunjuk oleh s, sehingga pemanggil sekarang melihat 100. Baris (2) membuat salihan sendiri dari metode yang menunjuk ke objek baru – s pemanggil tetap utuh – dan baris (3) hanya mempengaruhi objek baru itu. Setelah panggilan, s.getScore() adalah 100: mutasinya menempel, assigns ulangnya tidak.

3.7

Class Variables and Class Methods · ⁨Variabel Kelas dan Metode Kelas⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.7.A: Develop code to define behaviors of a class through class methods.

  • 3.7.A.1 Class methods cannot access or change the values of instance variables or call instance methods without being passed an instance of the class via a parameter.
  • 3.7.A.2 Class methods can access or change the values of class variables and can call other class methods.

Learning Objective 3.7.B: Develop code to declare the class variables that belong to the class.

  • 3.7.B.1 Class variables belong to the class, with all objects of a class sharing a single copy of the class variable. Class variables are designated with the static keyword before the variable type.
  • 3.7.B.2 Class variables that are designated public are accessed outside of the class by using the class name and the dot operator, since they are associated with a class, not objects of a class.
  • 3.7.B.3 When a variable is declared final, its value cannot be modified.
Bahasa Indonesia

Tujuan Pembelajaran 3.7.A: Mengembangkan kode untuk mendefinisikan perilaku kelas melalui metode kelas.

  • 3.7.A.1 Metode kelas tidak dapat mengakses atau mengubah nilai variabel instance atau memanggil metode instance tanpa dilemparkan sebuah instance dari kelas tersebut melalui parameter.
  • 3.7.A.2 Metode kelas dapat mengakses atau mengubah nilai variabel kelas dan dapat memanggil metode kelas lainnya.

Tujuan Pembelajaran 3.7.B: Mengembangkan kode untuk mendeklarasikan variabel kelas yang milik kelas.

  • 3.7.B.1 Variabel kelas milik kelas, dengan semua objek dari sebuah kelas berbagi satu salinan dari variabel kelas tersebut. Variabel kelas ditandai dengan kata kunci static sebelum tipe variabel.
  • 3.7.B.2 Variabel kelas yang ditunjuk public diakses di luar kelas dengan menggunakan nama kelas dan operator titik, karena mereka terkait dengan kelas, bukan objek dari kelas.
  • 3.7.B.3 Ketika sebuah variabel dideklarasikan final, nilainya tidak dapat dimodifikasi.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English
static vs instance fields

A static (class) variable 类变量, marked static, is shared by all objects of the class – one copy total (e.g. a counter of how many objects exist). A static method belongs to the class and cannot use instance fields directly. Access them by class name: Student.getCount().

Bahasa Indonesia
field statis vs instance

Sebuah variabel statis (kelas), ditandai static, dibagi oleh semua objek dari kelas – satu salinan total (misalnya counter berapa banyak objek yang ada). Metode statis milik kelas dan tidak dapat menggunakan field instance secara langsung. Akseslah mereka dengan nama kelas: Student.getCount().

3.8

Scope and Access · ⁨Ruang Lingkup dan Akses⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.8.A: Explain where variables can be used in the code.

  • 3.8.A.1 Local variables are variables declared in the headers or bodies of blocks of code. Local variables can only be accessed in the block in which they are declared. Since constructors and methods are blocks of code, parameters to constructors or methods are also considered local variables. These variables may only be used within the constructor or method and cannot be declared to be public or private.
  • 3.8.A.2 When there is a local variable or parameter with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable within the body of the constructor or method.
Bahasa Indonesia

Tujuan Pembelajaran 3.8.A: Menjelaskan di mana variabel dapat digunakan dalam kode.

  • 3.8.A.1 Variabel lokal adalah variabel yang dideklarasikan di header atau badan blok kode. Variabel lokal hanya dapat diakses di blok tempat mereka dideklarasikan. Karena konstruktor dan metode adalah blok kode, parameter untuk konstruktor atau metode juga dianggap sebagai variabel lokal. Variabel-variabel ini hanya dapat digunakan di dalam konstruktor atau metode dan tidak dapat dideklarasikan sebagai public atau private.
  • 3.8.A.2 Ketika ada variabel lokal atau parameter dengan nama yang sama dengan variabel instance, nama variabel akan merujuk ke variabel lokal alih-alih variabel instance di dalam badan konstruktor atau metode.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

Scope 作用域 is where a name is visible. A local variable declared in a method exists only inside it; a parameter exists only in its method; an instance variable is visible throughout the object. Access modifiers control visibility across classes: private (this class only) versus public (anywhere). Local variables shadow fields of the same name – a source of bugs.

Bahasa Indonesia

Ruang lingkup adalah tempat nama terlihat. Variabel lokal yang dideklarasikan dalam metode hanya ada di dalamnya; parameter hanya ada di metodenya; variabel instance terlihat di seluruh objek. Modulator akses mengontrol visibilitas melintasi kelas: private (hanya kelas ini) versus public (di mana saja). Variabel lokal menyelimuti field dengan nama yang sama – sumber bug.

Variabel global terlihat di mana saja; variabel lokal hanya di dalam bloknya
Variabel global terlihat di mana saja; variabel lokal hanya di dalam bloknya
Vocabulary · ⁨Kosa kata⁩ Train · ⁨Latih⁩
English Bahasa Indonesia
Scope/skəʊp/ Cakupan
3.9

The this Keyword · ⁨Kata Kunci this⁩

Syllabus · ⁨Silabus⁩
English

Learning Objective 3.9.A: Develop code for expressions that are self-referencing and determine the result of these expressions.

  • 3.9.A.1 Within an instance method or a constructor, the keyword this acts as a special variable that holds a reference to the current object—the object whose method or constructor is being called.
  • 3.9.A.2 The keyword this can be used to pass the current object as an argument in a method call.
  • 3.9.A.3 Class methods do not have a this reference.
Bahasa Indonesia

Tujuan Pembelajaran 3.9.A: Mengembangkan kode untuk ekspresi yang merujuk pada diri sendiri dan menentukan hasil dari ekspresi-ekspresi tersebut.

  • 3.9.A.1 Di dalam metode instance atau konstruktor, kata kunci this bertindak sebagai variabel khusus yang memegang referensi ke objek saat ini—objek yang metode atau konstruktornya sedang dipanggil.
  • 3.9.A.2 Kata kunci this dapat digunakan untuk melempar objek saat ini sebagai argumen dalam pemanggilan metode.
  • 3.9.A.3 Metode kelas tidak memiliki referensi this.

Source: College Board AP Course and Exam Description · ⁨Sumber: Deskripsi Kursus dan Ujian College Board AP⁩

English

this is a reference to the current object. Use it to tell a field apart from a parameter with the same name, or to call another method of the same object:

Exam skill: when a constructor or setter's parameter has the same name as a field, you must write this.field = param – without this, the assignment does nothing useful.

Bahasa Indonesia

this adalah referensi ke objek saat ini. Gunakan ini untuk membedakan field dari parameter dengan nama yang sama, atau untuk memanggil metode lain dari objek yang sama:

public Student(String name, int score) {
    this.name = name;      // this.name is the field; name is the parameter
    this.score = score;
}

Keterampilan ujian: ketika parameter konstruktor atau setter memiliki nama yang sama dengan field, Anda harus menulis this.field = param – tanpa this, penugasan tersebut tidak melakukan apa-apa yang berguna.

3.9

Exam tips · ⁨Tips ujian⁩

English
  • Design with methods and classes: encapsulate data as private fields and expose behaviour through public methods.
  • Know the difference between an object and its class, and that objects are passed by value — the parameter gets a copy of the reference, so a method can change the object's state, but reassigning the parameter does not affect the caller (Java has no pass-by-reference).
  • Traverse arrays and ArrayLists safely — size is length vs .size(), and removing during a loop shifts indices.
  • Trace a recursive method to determine its result: find the base case first, then follow each recursive call to its return value (writing recursive code is outside the exam's scope).
  • Recognise inheritance vocabulary — superclass, subclass, method overriding, and that every class is a subclass of Object (designing and implementing inheritance is outside the exam's scope).
Bahasa Indonesia
  • Rancanglah dengan metode dan kelas: enkapsulasi data sebagai private fields dan tampilkan perilaku melalui metode publik.
  • Pahami perbedaan antara objek dan kelasnya, serta fakta bahwa objek dikirim berdasarkan nilai — parameter mendapatkan salinan referensinya, sehingga metode dapat mengubah state objek, tetapi menetapkan ulang parameter tidak mempengaruhi pemanggil (Java tidak memiliki pengiriman berdasarkan referensi).
  • Jelajahi array dan ArrayList dengan aman — ukurannya adalah length vs .size(), dan penghapusan selama perulangan akan menggeser indeks.
  • Jejak metode rekursif untuk menentukan hasilnya: temukan kasus basis terlebih dahulu, lalu ikuti setiap panggilan rekursif hingga nilainya kembali (menulis kode rekursif berada di luar cakupan ujian).
  • Kenali kosakata pewarisan — superclass, subclass, method overriding, dan bahwa setiap kelas adalah subclass dari Object (merancang dan mengimplementasikan pewarisan berada di luar ruang lingkup ujian).

Interactive lessons on this topic · ⁨Pelajaran interaktif untuk topik ini⁩

Work through it step by step, with instant-check exercises. · ⁨Kerjakan langkah demi langkah, dengan latihan pengecekan instan.⁩

Past Papers · ⁨Soal-Soil Masa Lalu⁩

More topics in AP Computer Science A · ⁨AP Ilmu Komputer A⁩ · ⁨Topik lain dalam AP Computer Science A · ⁨AP Ilmu Komputer A⁩⁩

Log in or create account · ⁨Masuk atau buat akun⁩

IGCSE, A-Level & AP