I have read that VB.NET is a fully Object-Oriented Compatible language....but to my amazement I have found out this code in a Console Application....
Code:
Imports System.Console
Module Module1
Sub Main()
WriteLine("Hello from Visual Basic")
ReadLine()
End Sub
End Module
Now how can we access WriteLine() and ReadLine() without creating the object of Console class(as we have only IMPORTED it, NOT INHERITED it), because in Java even if u import a class in a package(namespace here) u have to still instantiate(create object of) that class to access its members(data and methods).......I am still a beginner to the .NET world(as i am still learning Console Application )
Last edited by Zaid; 01-21-2013 at 02:36 PM..
Reason: Forgot the [CODE]tag
in Java even if u import a class in a package(namespace here) u have to still instantiate(create object of) that class to access its members
Not true.
If the members are *STATIC* members, you SHOULD NOT use an instantiated object to access them. *AT MOST* you would use the class name as a prefix.
In other words:
Code:
// This is simplified Java code:
class Foo
{
static void Zap( String s ) { ... }
}
...
// then you should *NEVER* do
Foo f = new Foo( );
f.Zap("xyz");
// yes, it works, but it is VERY VERY misleading to do so!
// you should instead use
Foo.Zap("xyz");
The *SAME THING* applies with VB.NET (and with C# and with any other .NET language!).
You can *EITHER* do
Code:
System.Console.WriteLine("a message")
invoking the STATIC method WriteLine *OR* you can do
Code:
Imports System.Console
WriteLine("a message")
You need to keep the distinction between static and dynamic methods and members straight.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Java can do this as well, but you should use it *very* sparingly. Keeping (and spoofing) the Foo:
PHP Code:
package foo;
public class Fou { public static void Zap(String s) { System.out.println(s); } }
// And used: package foo;
import static foo.Fou.Zap;
public class Lu { public static void main(String[] argv) { Zap("doFou"); } }
I really would not recommend this with Java, and especially with custom classes. But for shortcut (ie: lazy ) usage, you can use it against the system.out for example, so you can just type in out.println. Unlike C#/VB, you can't get to the println this way as the PrintStream is an instance object and not a class.
BTW, default functionality in languages like Java already use an implicit static import for the entire java.lang package. Without it you'd need to import or instantiate all the base objects including object, and nobody wants to do new java.lang.Object();.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
import java.lang.System;
class Demo
{
public static void main(String args[])
{
out.println("Hello");
}
}
but it is not working.....so u said wrong....and plz don't try to confuse me(with all those PHP's and irrelevant ****) when u seriously don't know the answer...
import java.lang.System;
class Demo
{
public static void main(String args[])
{
out.println("Hello");
}
}
but it is not working.....so u said wrong....and plz don't try to confuse me(with all those PHP's and irrelevant ****) when u seriously don't know the answer...
I beg your pardon? Perhaps you are looking at my signature.
You are obviously not reading what I said:
Quote:
But for shortcut (ie: lazy ) usage, you can use it against the system.out for example, so you can just type in out.println. Unlike C#/VB, you can't get to the println this way as the PrintStream is an instance object and not a class.
You *must* have an instance of Java's PrintStream in order to invoke the println. The System.out is a static instance of PrintStream.
PHP Code:
import static java.lang.System.out; // You cannot statically import System. public class myclass { public static void main(String argv[]) { out.println("Hello world"); } }
Quote:
Originally Posted by Zaid
Didn't buy it....
Unlike Java, C# uses a static console and static WriteLine. You can import the System.Console directly and invoke the methods without referring to the class.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
If you don't understand why using an *instance* of a class is a VERY BAD way to invoke a static method, then you have not understood Java completely, at all.
Perhaps you should study Java fundamentals more.
Either that or simply stop trying to compare Java with VB.NET.
There are similarities, but they are *NOT* the same, as FouLu tried to show you.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.