0802.2024.Today I Learned

Sungju Kim·2024년 8월 4일

Sparta_Coding_Camp_TIL

목록 보기
9/53

The following is a summary of new content that I learned on August 2nd (one topic after another is not correlated).

Printing A Map (Object As Keys & Values)

The map that I wanted to print had the Subject class object as a key and a list of Score class objects as its value.

// for context, this is the map field 
private Map<Subject, List<Score>> subjectScores = new HashMap<>();

// printing out the map
System.out.println("Below is the printed map including its keys and values!");
for (Map.Entry<Subject, List<Score>> entry : subjectScores.entrySet()) {
	// get the key and value of each entry
    Subject subject = entry.getKey();
    List<Score> scoreList = entry.getValue();
    
    // print them out
    System.out.println("Subject: " + subject.getSubjectName());
    for (Score score : scoreList) {
        System.out.println("Score: " + score.getScore());
    }
}

Path vs. Query variable in queries

A query vairable is optional whereas a path variable is not.
For example in a Google search, if you don't fill in a query variable, you would be searching without a search keyword, which leads you back to the main page. Similarly, supposed that you initially limited your search to 'from May 15, 2023 to May 15, 2024'. Later on if you take out the beginning date in your query, you would simply get all the searches prior to May 15th, 2024.

// without search keyword
https://www.google.com/webhp?hl=en&sa=X&ved=0ahUKEwiP8YnPt9qHAxWAi68BHTdGIhgQPAgK

// with search key words "hello world"
https://www.google.com/search?q=hello+world&sca_esv=ee98ebe28384d87e&hl=en&sxsrf=ADLYWIJcO76j2s5NuZq4gON0AZvog8OknA%3A1722743832408&source=hp&ei=GPyuZu6MFouivr0P5KHfmQs&iflsig=AL9hbdgAAAAAZq8KKKr708-YF34plwHsz7ThWb8Dav1k&ved=0ahUKEwju-KrxuNqHAxULka8BHeTQN7MQ4dUDCBc&uact=5&oq=hello+world&gs_lp=Egdnd3Mtd2l6IgtoZWxsbyB3b3JsZDIIEAAYgAQYywEyCBAAGIAEGMsBMggQABiABBjLATIIEC4YgAQYywEyCBAAGIAEGMsBMggQABiABBjLATIIEAAYgAQYywEyCBAAGIAEGMsBMggQABiABBjLATIIEAAYgAQYywFI5GVQglhY5WRwAngAkAEAmAHdAaABng2qAQUwLjkuMbgBA8gBAPgBAZgCDKACuw2oAgrCAgcQIxgnGOoCwgINEC4Y0QMYxwEYJxjqAsICBBAjGCfCAgoQIxiABBgnGIoFwgIKEC4YgAQYJxiKBcICChAAGIAEGEMYigXCAgsQLhiABBjRAxjHAcICBRAAGIAEwgIFEC4YgATCAgoQABiABBgUGIcCwgILEC4YgAQYxwEYrwHCAg0QLhiABBhDGMkDGIoFwgILEAAYgAQYkgMYigXCAg4QLhiABBjRAxjHARjLAcICCxAuGIAEGNQCGMsBmAMF4gMFEgExIECSBwUyLjkuMaAHiHs&sclient=gws-wiz

Static Variables

  • A static variable is a variable that belongs to the class rather than to any specific instance of the class.
  • They can be called directly using the class name, without creating an instance of the class.
  • It is shared among all instances of the class, so any changes made to the variable are reflected across all instances.
  • a memory is allocated only once and thus the memory address that does not change through the program.

Static Methods

  • Similar to a static variable, a static method belongs to the class rather than a specific instance of the class.
  • They can be called directly using the class name, without creating an instance of the class.
  • Since static methods are not tied to any specific object, they cannot access instance (non-static) variables or methods directly. They can only interact with static members of the class.
    • AKA static methods cannot access instance variables or instance methods directly because they do not have access to the instance (this).

💡 This would explain why when you write functions within the main method, you will often get errors telling you to change your method to static!

Do static methods run automatically like the main()?

- No!

Because the main function that starts our program is static, some people seem to think that static methods runs automatically without being called. However, static methods also do not run automatically unless they are explicitly called. The main method is called by the Java runtime environment as the starting point of the program. Therefore, other static methods will only run if they are called within main or another method.

However!

Static variables and methods in Java get memory allocation even if they are not explicitly referenced or called in the program. That's why even if no instance of the class is ever created, the static variable can still be accessed using the class name because it exists as soon as the class is loaded.

profile
Fully ✨committed✨ developer, always eager to learn!

0개의 댓글