Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Java's notion of static different from C#'s?

Tags:

java

c#

static

I am reading Josh Bloch's book Effective Java and he suggests using a builder design pattern when building objects that have large amounts of members. From what I can see it isn't the vanilla design pattern but looks like his variation. I rather like the look of it and was trying to use it in a C# web application that I am writting. This is the code written in Java and works perfectly

public class Property { 

    private String title;
    private String area;

    private int sleeps = 0;

    public static void main(String[] args) {

        Property newProperty = new Property.Builder("Test Property").Area("Test Area").Sleeps(7).build();

    }

    private Property(Builder builder) {
        this.title = builder.title;
        this.area = builder.area;
        this.sleeps =builder.sleeps;
    }

    public static class Builder{
        private String title;
        private String area;

        private int sleeps = 0;

        public Builder (String title){
            this.title = title;
        }

        public Builder Area(String area){
            this.area = area;
            return this;
        }

        public Builder Sleeps(int sleeps){
            this.sleeps = sleeps;
            return this;
        }

        public Property build() {
            return new Property(this);
        }
    }   
}

When I put this into what I think is the C# equivalent

 public class Property
    {
    private String title;
    private String area;

    private Property(Builder Builder)
    {
        title = Builder.title;
        area = Builder.area;
    }


    public static class Builder
    {
        // Required parameters
        private String title;
        private String area;

        // Optional parameters
        private int sleeps = 0;

        public Builder(String val)
        {
            this.title = val;
        }

        public Builder Area(String val)
        {
            this.area = val;
            return this;
        }

        public Builder Sleeps(int val)
        {
            this.sleeps = val;
            return this;
        }

        public Property build()
        {
            return new Property(this);
        }
    }
    }

Then I get compiler warnings. Most of them "cannot declare instance members in a static class".

So my question is firstly what have I missed? If I have missed something, can I do it in the manner Josh Bloch recommends but in C#, and lastly, and this one important too, is this thread-safe?

like image 876
uriDium Avatar asked Feb 04 '09 18:02

uriDium


People also ask

What is the difference between static in C and C++?

If (as your comment to the question indicates) this is the only use of static you're concerned with then, no, there is no difference between C and C++. When used within a function, it sets the duration of the item.

Is static is same in Java and C?

The static data members are basically same in Java and C++. The static data members are the property of the class, and it is shared to all of the objects.

What is the difference between static and non static variables in C?

Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.

How Java differs from C explain?

C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).


1 Answers

public static class in Java means that you define a static nested class. That means that it is logically contained in another class but instances of it can exist without a reference to it's outer class. A non-static nested class is called an "inner class" and instances of it can only ever exist in relation to an instance of the outer class.

In C# a static class is one that can't be instantiated and thus can't have any non-static members. There is no direct language-level equivalent to this construct in Java, but you can easily prevent instantiation of a Java class by providing only a private constructor.

Short Java recap:

  • All Classes defined inside another Class are "nested Classes"
  • nested Classes that are not static are called inner Classes
  • instances of inner Classes can only exist in relation to an instance of the outer Class
  • static nested Classes have no separate name
  • static nested Classes are largely independent from their outer class (except for some privileged access).

I'd be happy if some C# guru told us how inner/nested classes are handled in C#/.NET.

like image 50
Joachim Sauer Avatar answered Oct 08 '22 14:10

Joachim Sauer