Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Strange

  1. #1
    New Coder
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Strange

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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();.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    so you can just type in out.println.
    I use the code
    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...

  • #5
    New Coder
    Join Date
    Sep 2012
    Location
    Mumbai
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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....

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Zaid View Post
    I use the code
    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...
    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.
    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 View Post
    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.
    Last edited by Fou-Lu; 01-28-2013 at 04:02 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by Zaid View Post
    Didn't buy it....
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •