...

View Full Version : Strange



Zaid
01-21-2013, 03:35 PM
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....



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 :p)

Old Pedant
01-21-2013, 09:24 PM
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:


// 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


System.Console.WriteLine("a message")
invoking the STATIC method WriteLine *OR* you can do


Imports System.Console
WriteLine("a message")


You need to keep the distinction between static and dynamic methods and members straight.

Fou-Lu
01-22-2013, 05:39 AM
Java can do this as well, but you should use it *very* sparingly. Keeping (and spoofing) the Foo:


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

Zaid
01-28-2013, 03:50 PM
so you can just type in out.println.
I use the code


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... :rolleyes:

Zaid
01-28-2013, 03:55 PM
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.

Didn't buy it.... :confused:

Fou-Lu
01-28-2013, 04:50 PM
I use the code


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... :rolleyes:

I beg your pardon? Perhaps you are looking at my signature.
You are obviously not reading what I said:


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.


import static java.lang.System.out; // You cannot statically import System.
public class myclass
{
public static void main(String argv[])
{
out.println("Hello world");
}
}



Didn't buy it.... :confused:

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.

Old Pedant
01-28-2013, 10:27 PM
Didn't buy it.... :confused:

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum