EzDevInfo.com

static interview questions

Top static frequently asked interview questions

Can I add extension methods to an existing static class?

I'm a fan of extension methods in C#, but haven't had any success adding an extension method to a static class, such as Console.

For example, if I want to add an extension to Console, called 'WriteBlueLine', so that I can go:

Console.WriteBlueLine("This text is blue");

I tried this by adding a local, public static method, with Console as a 'this' parameter... but no dice!

public static class Helpers {
    public static void WriteBlueLine(this Console c, string text)
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine(text);
        Console.ResetColor();
    }
}

This didn't add a 'WriteBlueLine' method to Console... am I doing it wrong? Or asking for the impossible?


Source: (StackOverflow)

Why doesn't Java allow overriding of static methods?

Why is it not possible to override static methods?

If possible, please use an example.


Source: (StackOverflow)

Advertisements

C++ static constant string (class member)

I'd like to have a private static constant for a class (in this case a shape-factory). I'd like to have something of the sort.

class A {
   private:
      static const string RECTANGLE = "rectangle";
}

Unfortunately I get all sorts of error from the C++ (g++) compiler, such as:

ISO C++ forbids initialization of member ‘RECTANGLE’

invalid in-class initialization of static data member of non-integral type ‘std::string’

error: making ‘RECTANGLE’ static

This tells me that this sort of member design is not compliant with the standard. How do you have a private literal constant (or perhaps public) without having to use a #define directive (I want to avoid the uglyness of data globality!)

Any help is appreciated. Thanks.


Source: (StackOverflow)

Why can't I inherit static classes?

I have several classes that do not really need any state. From the organizational point of view, I would like to put them into hierarchy.

But it seems I can't declare inheritance for static classes.

Something like that:

public static class Base
{
}

public static class Inherited : Base
{
}

will not work.

Why have the designers of the language closed that possibility?


Source: (StackOverflow)

Change private static final field using Java reflection

I have a class with a private static final field that, unfortunately, I need to change at run-time.

Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field

Is there any way to change the value?

Field hack = WarpTransform2D.class.getDeclaredField("USE_HACK");
hack.setAccessible(true);
hack.set(null, true);

Source: (StackOverflow)

Getting the class name from a static method in Java

How can one get the name of the class from a static method in that class. For example

public class MyClass {
    public static String getClassName() {
        String name = ????; // what goes here so the string "MyClass" is returned
        return name;
    }
}

To put it in context, I actually want to return the class name as part of a message in an exception.


Source: (StackOverflow)

How do you create a static class in C++?

How do you create a static class in C++? I should be able to do something like:

cout << "bit 5 is " << BitParser::getBitAt(buffer, 5) << endl;

Assuming I created the BitParser class. What would the BitParser class definition look like?


Source: (StackOverflow)

ReSharper warns: "Static field in generic type"

public class EnumRouteConstraint<T> : IRouteConstraint
    where T : struct
{
    private static readonly Lazy<HashSet<string>> _enumNames; // <--

    static EnumRouteConstraint()
    {
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException(Resources.Error.EnumRouteConstraint.FormatWith(typeof(T).FullName));
        }

        string[] names = Enum.GetNames(typeof(T));
        _enumNames = new Lazy<HashSet<string>>(() => new HashSet<string>
        (
            names.Select(name => name), StringComparer.InvariantCultureIgnoreCase
        ));
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        bool match = _enumNames.Value.Contains(values[parameterName].ToString());
        return match;
    }
}

Is this wrong? I would assume that this actually has a static readonly field for each of the possible EnumRouteConstraint<T> that I happen to instance.


Source: (StackOverflow)

private final static attribute vs private final attribute

In Java, what's the difference between:

private final static int NUMBER = 10;

and

private final int NUMBER = 10;

Both are private and final, the difference is the static attribute.

What's better? And why?


Source: (StackOverflow)

Adding a favicon to a static HTML page

I have a few static pages that are just pure HTML, that we display when the server goes down. How can I put a favicon that I made (it's 16x16px and it's sitting in the same directory as the HTML file; it's called favicon.ico) as the "tab" icon as it were. I followed the rules I have read up on and no dice. I read a post that suggested the following as a link in the head section of the pages.

<link rel="icon" rel='nofollow' href="favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" rel='nofollow' href="favicon.ico" type="image/x-icon"/>

But it still doesn't want to work. I am using Chrome to test the sites. According to Wikipedia .ico is the best pic format that runs on all browser types.


Source: (StackOverflow)

Static Block in Java [duplicate]

This question already has an answer here:

I was looking over some code the other day and I came across:

static {
    ...
}

Coming from C++, I had no idea why that was there. Its not an error because the code compiled fine. What is this "static" block of code?


Source: (StackOverflow)

What does the 'static' keyword do in a class?

To be specific, I was trying this code:

package hello;

public class Hello {

    Clock clock = new Clock();

    public static void main(String args[]) {
        clock.sayTime();
    }
}

But it gave the error

Cannot access non-static field in static method main

So I changed the declaration of clock to this:

static Clock clock = new Clock();

And it worked. What does it mean to put that keyword before the declaration? What exactly will it do and/or restrict in terms of what can be done to that object?


Source: (StackOverflow)

Java: Static vs non static inner class [duplicate]

This question already has an answer here:

What is the difference between static and non static inner class?


Source: (StackOverflow)

New self vs. new static

I am converting a PHP 5.3 library to work on PHP 5.2. The main thing standing in my way is the use of late static binding like return new static($options); , if I convert this to return new self($options) will I get the same results?

What is the difference between new self and new static?


Source: (StackOverflow)