There is the following test task:

Specify the correct ways to get a reference to the Class object of the String class.

  1. Unable to get reference to Class object of class String, since this class is final;
  2. String.class;
  3. Class.forName ("java.lang.String")
  4. new String.getClass ()

I bow to 2) and 4), but not sure.

Closed due to the fact that the participants are not on topic by Sergey Gornostaev , default locale , 0xdb , Jarvis_J , Eugene Krivenja Jun 30 '18 at 8:28 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Sergey Gornostaev, Jarvis_J, Eugene Krivenja
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    And why not try to make a program? And get rid of all the options? - Chubatiy
  • one
    new String.getClass() does not even compile. Maybe there still was a new String().getClass() ? - Regent
  • that's right, there was new String (). getClass () - basketplayer
  • Questions with multiple-choice questions, perhaps, offtopic. Each of the options should be disassembled separately. Otherwise it turns out four questions in one. PS You can fix your question using the β€œEdit” button - default locale

1 answer 1

Sample code with comments:

 /* ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: ΠŸΠ°ΠΊΠ΅Ρ‚ java.lang.ref Помимо ΠΎΠ±Ρ‹ΠΊΠ½ΠΎΠ²Π΅Π½Π½Ρ‹Ρ… ссылок, извСстных Ρ‚Π°ΠΊ ΠΆΠ΅ ΠΊΠ°ΠΊ Strong References (ΡΠΈΠ»ΡŒΠ½Ρ‹Π΅ ссылки), Π² Java прСдставлСн ΠΏΠ°ΠΊΠ΅Ρ‚ java.lang.ref, содСрТащий Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ Soft (мягких), Week (слабых) ΠΈ Phantom (Ρ„Π°Π½Ρ‚ΠΎΠΌΠ½Ρ‹Ρ…) ссылок, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΏΠΎ-Ρ€Π°Π·Π½ΠΎΠΌΡƒ ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°ΡŽΡ‚ΡΡ сборщиком мусора ΠΈ ΠΏΡ€Π΅Π΄ΠΎΡΡ‚Π°Π²Π»ΡΡŽΡ‚ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŽ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡ‚ΡŒ Π±ΠΎΠ»Π΅Π΅ Π³ΠΈΠ±ΠΊΠΎ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ с ΠΏΠ°ΠΌΡΡ‚ΡŒΡŽ. */ package regObjClass; import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; /** * * @author vvm */ public class RefObjClassString { public static void main(String[] args) throws ClassNotFoundException { Class c = Class.forName("java.lang.String"); //Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ экзСмпляр класса //Если Ρƒ нас Π΅ΡΡ‚ΡŒ класс, для ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ компиляции извСстСн Ρ‚ΠΈΠΏ Ρ‚ΠΎ Class strClass = String.class; /* мягкиС ΠΈ Ρ„Π°Π½Ρ‚ΠΎΠΌΠ½Ρ‹Π΅ ссылки*/ /* созданиС мягкой ссылки */ SoftReference<String> softRefStr = new SoftReference<String>(new String("SoftRef")); ReferenceQueue<String> queue = new ReferenceQueue<String>(); //созданиС ΠΎΡ‡Π΅Ρ€Π΅Π΄ΠΈ /*созданиС Ρ„Π°Π½Ρ‚ΠΎΠΌΠ½ΠΎΠΉ ссылки*/ PhantomReference<String> ref = new PhantomReference<String>(new String("Π€Π°Π½Ρ‚ΠΎΠΌ"), queue); } } 
  • Those. it turns out that the answers will be correct 2), 3), 4)? - basketplayer
  • The getClass () method allows you to get the type of this object. - Vyacheslav Mischenko 4:19 pm
  • The getClass () method allows you to get the type of this object. In my opinion the answers are 2) 3) correct. My code above is only an addition to the answer. But still, the question requires clarification, the context of using the "correct" ways to get the link and the java version. String s = String.class.newInstance (); // Create a new object of class String. Some related topics are: Reflection, ClassLoader - Vyacheslav Mischenko
  • And where are the soft and phantom links? - default locale
  • To give a more complete answer to the question. Documentation links: docs.oracle.com/javase/7/docs/api/java/lang/ref/Reference.html Object link types: quizful.net/post/java_reference_types Link (Java): cyclowiki.org/wiki/ Link_ (Java) - Vyacheslav Mischenko