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

  • SEARCH
  • Home
  • 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 8583025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:26:17+00:00 2026-06-11T21:26:17+00:00

I have a non-static class called ImplementHeaderButtons which contains a non-static public method called

  • 0

I have a non-static class called ImplementHeaderButtons which contains a non-static public method called Implement. The name of the class and method are not important, what’s important is that they are not static, so they need to be instantiated in order to be used, right?

So I used to do this:

var implementHeaderButtons = new ImplementHeaderButtons();
implementHeaderButtons.Implement(this, headerButtons);

But then I decided to play around a bit with it (actually I was looking for a way to make it a one-liner) and I concluded that the following code works just as well:

new ImplementHeaderButtons().Implement(this, headerButtons);

Now, I do not need a variable to hold the instance, but my question is: how come I can create a new instance of a class on the fly and call a method of it without having a variable to store the instance?

I wouldn’t be surprised if it didn’t work as intended, but it does.

  • 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-06-11T21:26:18+00:00Added an answer on June 11, 2026 at 9:26 pm

    they are not static, so they need to be instantiated in order to be used, right?

    Yes, but you are still instantiating the class with new ImplementHeaderButtons(), you just aren’t storing a reference to that newly created instance anywhere.

    You can still call a method on this instance as you have done in your example, but you will not be able to do anything else with it afterwards without a reference. Eventually the instance will be cleaned up by the garbage collector (provided the method you call does not store a reference to the object somewhere).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a private static method called Inst() which allows the class to
I have an Error class that has public static method db_log_error() and private method
I have a class (called Employee. non static) defined in a class library. I
I have a static field in a non static class. public class DBtools {
So in java, say you have a non-static method 'bar()' in an class 'Foo'.
In PHP, is it possible to have a function within a class that's non-static,
I have a recursive variadic templated method that is called from a non-recursive variadic
I have a base class that declares a private non-static reference to the DataBase
I have a class called ColorConstants which is simply a class that defines a
My problem is the following; I have a class that uses non-static native methods

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.