I did search briefly but not really sure what I'm asking. Just dipping my feet in to Java and OOP.
Take this declaration:
SomeClass myClass = new SomeClass();
in PHP (which is what I have most experience with), you would just do:
$myClass = new SomeClass
So why the difference? What does the "prefix" of SomeClass
do, that in PHP it isn't necessary开发者_开发技巧.
Am I right in that in the first example, I am declaring a class of type "SomeClass", much the same way you would declare: int myInt;
Hope this makes sense. Thanks.
edit also not trying to enter a PHP/Java debate, merely that PHP is a language I am familiar with!
In short yes.
You are saying that you want a variable myClass
of type SomeClass
and initializing it to the value of new SomeClass()
Longer Explination:
The reason that you do this is that it lets the compiler, IDE and whatever else know that myClass
is a SomeClass
.
Short Example:
public class SomeClass(){
public int SomeInt = 0;
public string SomeString = "";
public SomeClass(string initialString)
{
SomeString = initialString;
}
public SomeClass(int initalValue)
{
SomeInt = initialValue;
}
}
now you can create a SomeClass and do things to initialize parts of it.
//Initialize a new instance of SomeClass with someString set to test
SomeClass setSomeString = new SomeClass("test");
//Initialize a new instance of SomeClass with SomeInt set to 4
SomeClass setSomeInt = new SomeClass(4);
Alternatively you could just do //Initialize a new instance of SomeClass and then set the values SomeClass myClass = new SomeClass(); myClass.SomeInt = 4; myClass.SomeString = test; Sure this is a very simple example but it shows what you are doing and what you can do with it.
As it is pointed out this is known as Static Typing, in that you must declare what a objects type is at compile time. PHP uses dynamic typing, which means that it determines the type of an object at run-time.
In Java; however, you can use the var
keyword to have it dynamically determine the Object type.
Java is a 'strongly typed language'. PHP is not.
Go search on that phrase and you will have hours of entertainment learning about what's good, bad, and indifferent about this. And you will find that any 3 people have 5 opinions.
There you are declaring a new variable myClass
of type SomeClass
, and then asigning to that variable a new object of that class with new SomeClass()
.
Maybe it's more clear the difference written like this.
// Declares myClass to be of type SomeClass but without initializing it.
SomeClass myClass;
// Creates a new object of the class SomeClass and asigns it to myClass
myClass = new SomeClass();
As bmargulies said, java is a Strongly typed language, and you have to declare the type of a variable before using it. This allows the compiler to do type checking at compilation time, wich in many cases can prevent programing mistakes.
The difference is that Java is statically-typed, while PHP is dynamically typed.
This (in short) means that java in order to be able to invoke methods on a given object the compiler must know the type of the object. And it will tell you whether you are allowed to invoke the method or not.
In PHP this check is performed at runtime (later), so there is no need for a compiler (not that there's any) to perform any checks.
What that line is doing is creating a variable of type SomeClass. In other words, you are creating an instance of the class SomeClass which is not the same as declaring a class. A class is declared like this:
public class SomeClass {
}
精彩评论