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?


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

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();

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.

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

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.

Need Your Help

Websocket not working in android app using phonegap

javascript android cordova websocket signalr

I am trying to establish a websocket connection between my signalR server and an android app, built using the phonegap CLI.

Web Services, Memory Leaks and CRM

web-services memory-management memory-leaks dynamics-crm

I have a website that allows users to upload a csv file. This calls a service that reads the information from the csv, puts it into DynamicEntity objects and calls the CRM service to Create/Update

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.