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 6771221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:24:24+00:00 2026-05-26T15:24:24+00:00

I am experimenting here a bit. Say I have a class : static class

  • 0

I am experimenting here a bit.

Say I have a class :

static class MyClass {
    static String property = "myProperty";
}

and a method:

public static void myMethod0(Class<MyClass> clazz)  {
   try {
        MyClass myClass = clazz.newInstance();
        System.out.println (myClass.property);
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

and to test it:

public static void main(String[] args ) {
    myMethod0(MyClass.class);
}

myMethod0 would work here, however, I am creating a new instance in order to reach the property.
Since the properties are static I should be able to reach them without actually creating any instance. For example as you would do when reaching a static property, ie MyClass.property

To summarize:
Is it possible to reach the static property of MyClass, by having Class clazz = MyClass.class ?

Thanks !

**

EDIT:

**
To put the above in perspective and what I am actually trying to accomplish:

public static class PDF_1 { public static PDF_1 it = new PDF_1();
    static String contentType = "application/pdf";
    static String fileEnding = "pdf";
}
static void myMethod0(PDF_1 pdf) {
    System.out.println(pdf.fileEnding);
}


public enum PDF_2 {it;
    static String contentType = "application/pdf";
    static String fileEnding = "pdf";        
}
static void myMethod1(PDF_2 pdf) {
   System.out.println(pdf.fileEnding);
}  

public static void main(String[] args ) {
    myMethod0(PDF_1.it); // Works fine! However very verbose because of public static PDF_1 it = new PDF_1();
    myMethod1(PDF_2.it); // Works fine! Somewhat verbose though because of the "it" keyword        
}

The whole idea as to what I am trying to accomplish with this, is that too often do I see people declare lots of strings, ie:

static class Constants {
    static String PDF_CONTENT_TYPE = "application/pdf";
    static String PDF_FILE_ENDING = "pdf";

    static String HTML_CONTENT_TYPE = "text/html";
    static String HTML_FILE_ENDING = "html";
}

// There is no way knowing what type the method actually wants. Is it contentType, fileEnding or something entirely different ? 
public void myMethod(String str) {

}

What I am trying to achieve is something that would allow you to pass a main class/enum, ie: PDF and the method will itself determine what it will use. The caller will just know what to pass, a PDF or HTML class/enum. I am also looking for something that is this refactor friendly as well. It is also of interest not to complicate the declaration of this creation. I find fully blown enums just as obtrusive as a class, and can be hard to read. The ide a is that I am just grouping the two strings in a parent object “PDF” and “HTML”. An enum:

public enum SomeType {
    PDF("application/pdf", "pdf"), HTML(...);
    String contentType;
    String fileEnding;        
    // Constructor ... 
}

Would not allow you to declare a method and specify that this method expects the HTML stuff. Only that the enum type is of type SomeType. There is a risk that “someone” would pass SomeType.PDF to that method. What I am doing with the enum and class seems like a noob solution, and the Java language should provide a feature like this, or does it already?

Does this make sense?

  • 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-26T15:24:24+00:00Added an answer on May 26, 2026 at 3:24 pm
    public enum PDF { it;
        static String contentType = "application/pdf";
        static String fileEnding = "pdf";        
    }
    static void myMethod1(PDF pdf) {
       System.out.println(pdf.fileEnding);
    }  
    
    public static void main(String[] args ) {
        myMethod1(PDF.it); // Works fine! Somewhat verbose though because of the "it" keyword. Not 100% ideal!      
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

noob here still experimenting with templates. Trying to write a message processing class template
Bit of an abstract problem here. I'm experimenting with the Domain Model pattern, and
I'm experimenting with WPF animations, and I'm a bit stuck. Here's what I need
I was experimenting with the splice() method in jconsole a = [1,2,3,4,5,6,7,8,9,10] 1,2,3,4,5,6,7,8,9,10 Here,
I'm experimenting with the code I found here The Java Specialists' Newsletter . public
I have been experimenting with WP7 apps today and have hit a bit of
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
i'm experimenting a bit with C# and XNA. Going through the advanced Riemers tutorials
I have been experimenting with PulpCore, trying to create my own tower defence game
While experimenting a bit with C++ templates I managed to produce this simple 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.