DEV Community

Carin Chapman
Carin Chapman

Posted on

I'm a JavaScript Developer who's learning Java

Despite their names, JavaScript and Java have less in common than you'd expect. Yes, they're both programming languages, and yes that of course means that they use a lot of the same underlying ideas (as do all programming languages), but when I recently started learning Java, I found a whole new class-based system that JavaScript doesn't use.

If you're familiar with the Object Oriented programming paradigm then you'll know that JavaScript kinda mimics classes sometimes. Like this for example, which is creating a constructor function for a puppy in pseudoclassical style:

const Puppy = function() {
this.age = 0;
this.legs = 4; = "puppy kibble";
}; = function(){

window.Puppy = Puppy;

From there, you can make an adult dog that inherits all the properties of the puppy, and--through the powers of polymorphism--you can change/delete/add to those properties:

const Dog = function() {;
this.age = 5; = "big dog kibble"

Dog.prototype = Object.create(Puppy.prototype);
Dob.prototype.constructor = Dog;

window.Dog = Dog;

That's essentially the JavaScript way of creating pseudo-classes that inherit from constructor functions. You can even go on to make another "class" of "senior dogs" that inherits everything from puppy and/or dog and adds stuff like = "old fart kibble"

If that looks familiar to you, then good news! You pretty much already understand what classes are going to be like in Java.
Java hinges on classes. In this language, everything is a class and classes include constructors that are used to create object instances of a class. Here's a pretty stripped down version to show you what I mean:

public class Student {
//these private variables will be used by the constructor, and
//therefore by all instances of the objects created by the
private int id;
private String name;
private int grade;

```//this is a student constructor, which creates a new student 
    //object (when called w/new keyword, compiler creates new 
```public Student(int id, String name, int grade) { = id; = name;
    this.grade = grade;

Here what we've done is to make a way for a Student object to exist. All student objects will have the properties that the constructor has: an id, a name, and a grade.
If we want the students to have teachers, we'll also have to create a teacher class with a teacher constructor to make teacher objects, like this:

public class Teacher {
private int id;
private String name;

```public Teacher(int id, String name) { = id; = name;

Again, all the instances of a Teacher will be an object with the properties of id and name.
Now to make students and teachers, inside a Main class, we'd do something like this:

Student Billy = new Student(1, "Billy", 12);
Student Sam = new Student(2, "Sam", 4);

Teacher Carin = new Teacher(1, "Carin");
Teacher Matt = new Teacher(2, "Matt");

Notice there that we're passing in values for each of the parameters we set up in the constructor function (e.g. id, name, and grade for the students or id and name for the teachers).

This is the beginning of the basics of how class works in Java, and how it's sounds similar, but is oh-so-different than the pseudo-classes in JavaScript.

Top comments (0)