Dr Hydralisk
Dr Hydralisk

Reputation: 1181

Creating objects in Java question

In PHP, to create a new object you would do something like this, $dog = new Dog;. But in Java you would do something like, Dog x = new Dog; or Dog x;. Could someone explain why you need to say the Dog class in front of the variable?

Upvotes: 5

Views: 1287

Answers (6)

ashwini
ashwini

Reputation: 140

your class name object name = new class name(); For Example

class Animal(){

//your code }

Animal dog = new Animal();

Upvotes: 0

Zak
Zak

Reputation: 7078

Thats java syntax for creating an object of a class, also called as creating an instance.

For example, Dog adog; where adog is a reference variable of type Dog.

Dog adog=new Dog(); where adog is a reference variable of type Dog which is now referring to the Dog object in memory.

And you use the dot(.) operator to access the object's instance variables and methods.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503869

Java statically and explicitly typed.

The type of the variable may be different from the type of the value it holds, due to inheritance. For example:

Animal dog = new Dog();

Here the type of the dog variable is Animal, but the value it holds is a reference to an instance of Dog.

Now in some other languages (C# 3 being the obvious example as a near neighbour of Java) you can use implicitly typed local variables when you actually want the type of a local variable to be the same as the type of the expression used to initialize it:

var dog = new Dog(); // Equivalent to Dog dog = new Dog();

(The type inferencing capabilities of some other statically typed languages go well beyond this.)

So, to go back to your original question, the answer is:

  • The dog variable has a type which is known at compile-time; this isn't true in PHP
  • The type of the variable has to be explicitly stated in Java; this is related to static typing, but isn't a requirement of static typing

Upvotes: 5

Desintegr
Desintegr

Reputation: 7090

You need to precise the type because Java is a strong and static typed language.

If you declare x as a Dog, it can only be a Dog or a subclass of Dog.

Another example :

public class Animal {
}

public class Dog extends Animal {
}

public class Cat extends Animal {
}

The following code is valid because x is declared as Animal, it can be a Dog or a Cat, or any subclass of Animal :

Animal x;
x = new Dog();
x = new Cat();

Upvotes: 6

fastcodejava
fastcodejava

Reputation: 41127

I think it is because Java is strictly typed. You need to put the type of the object at compile time.

Upvotes: 0

Peter Lang
Peter Lang

Reputation: 55614

In Java you would call the constructor like this (in case it has no parameters):

Dog x = new Dog();

By writing the class-name in front of the variable, you tell Java that your variable x is of type Dog. This allows Java to find out you're doing something wrong when writing:

Dog x = new Cat();

See PHP Variables at w3schools:

PHP is a Loosely Typed Language

In PHP, a variable does not need to be declared before adding a value to it.

In the example above, you see that you do not have to tell PHP which data type the variable is.

PHP automatically converts the variable to the correct data type, depending on its value.

In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.

In PHP, the variable is declared automatically when you use it.

Upvotes: 1

Related Questions