Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 970425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:48:10+00:00 2026-05-16T02:48:10+00:00

One use of the var keyword in C# is implicit type declaration. What is

  • 0

One use of the var keyword in C# is implicit type declaration. What is the Java equivalent syntax for var?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:48:11+00:00Added an answer on May 16, 2026 at 2:48 am

    There is none. Alas, you have to type out the full type name.

    Edit: 7 years after being posted, type inference for local variables (with var) was added in Java 10.

    Edit: 6 years after being posted, to collect some of the comments from below:

    • The reason C# has the var keyword is because it’s possible to have Types that have no name in .NET. Eg:

      var myData = new { a = 1, b = "2" };
      

      In this case, it would be impossible to give a proper type to myData. 6 years ago, this was impossible in Java (all Types had names, even if they were extremely verbose and unweildy). I do not know if this has changed in the mean time.

    • var is not the same as dynamic. variables are still 100% statically typed. This will not compile:

      var myString = "foo";
      myString = 3;
      
    • var is also useful when the type is obvious from context. For example:

      var currentUser = User.GetCurrent();
      

      I can say that in any code that I am responsible for, currentUser has a User or derived class in it. Obviously, if your implementation of User.GetCurrent return an int, then maybe this is a detriment to you.

    • This has nothing to do with var, but if you have weird inheritance hierarchies where you shadow methods with other methods (eg new public void DoAThing()), don’t forget that non-virtual methods are affected by the Type they are cast as.

      I can’t imagine a real world scenario where this is indicative of good design, but this may not work as you expect:

      class Foo {
          public void Non() {}
          public virtual void Virt() {}
      }
      
      class Bar : Foo {
          public new void Non() {}
          public override void Virt() {}
      }
      
      class Baz {
          public static Foo GetFoo() {
              return new Bar();
          }
      }
      
      var foo = Baz.GetFoo();
      foo.Non();  // <- Foo.Non, not Bar.Non
      foo.Virt(); // <- Bar.Virt
      
      var bar = (Bar)foo;
      bar.Non();  // <- Bar.Non, not Foo.Non
      bar.Virt(); // <- Still Bar.Virt
      

      As indicated, virtual methods are not affected by this.

    • No, there is no non-clumsy way to initialize a var without an actual variable.

      var foo1 = "bar";        //good
      var foo2;                //bad, what type?
      var foo3 = null;         //bad, null doesn't have a type
      var foo4 = default(var); //what?
      var foo5 = (object)null; //legal, but go home, you're drunk
      

      In this case, just do it the old fashioned way:

      object foo6;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why one would use one of the following packages instead of the other? Java
In other words may one use /<tag[^>]*>.*?<\/tag>/ regex to match the tag html element
When should one use a scripting language over a more verbose, compiled language like
A coworker of mine mentioned that one use of XSLT is processing business rules.
Query parameters : http://example.com/apples?order=random&color=blue Matrix parameters : http://example.com/apples;order=random;color=blue When should one use query parameters
Why use one over the other?
I need to use one of my VB.NET projects in a C++ project. The
I need to use one logical PGM based multicast address in application while enable
And why would I use one over the other in my code?
I'm wondering how one could use template databinding to accomplish what the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.