Skip to main content

Static and Non-Static

 

What is the purpose of the static keyword in Java?

The static keyword is used to declare class-level variables and methods. They belong to the class rather than instances of the class and can be accessed using the class name.

can we use the static keyword for package level access?

In Java, the static keyword is not used for controlling access levels. The static keyword is primarily used to define class-level members, such as variables and methods, that are associated with the class itself rather than with instances of the class. The concept of access levels, on the other hand, is controlled by access modifiers.

public class MyClass {

    public static int publicStaticVariable = 10;

    static int packagePrivateStaticVariable = 20; // package-private by default

    private static int privateStaticVariable = 30;

}

To summarize, while static is used to define class-level members, access modifiers (public, protected, default, private) are used to control the visibility and accessibility of those members. The use of static and access modifiers is independent of each other.

It's important to note that while static members have their use cases, they should be used judiciously, as overuse of static members can lead to code that is tightly coupled and less maintainable. Instance-specific members (non-static) are more common in object-oriented programming, where each object has its state and behavior.

does instance of the classes are non-static?

Instances of a class are non-static. When we create objects (instances) of a class in Java, each object has its own set of instance variables and instance methods. These are attributes and behaviour’s that are specific to each individual object.

Comments

Popular posts from this blog

A Well Comprehensive Test plan for Manual Testing

  Scope Definition: Define the scope based on specifications and requirements provided. However, also include exploratory testing to uncover issues that might not be explicitly mentioned in the requirements. Consider factors like usability, accessibility, security, and performance to broaden the scope beyond just functional testing. Considering factors like usability, accessibility, and security in the scope definition is essential for ensuring a comprehensive testing approach that goes beyond functional requirements Test Case Design: Use a combination of traditional methods like boundary value analysis, equivalence partitioning, and decision tables for structured testing. Incorporate exploratory testing techniques to explore the application and identify unexpected behaviour or edge cases. Prioritize test cases based on risk assessment and criticality. can implement the shift-left approach in...

Pre conditions and Post conditions in Test Ng.

Pre conditions and PostConditions in Test Ng. Vision of Preconditions and Postconditions: 👉 Ensuring Test Isolation: By using preconditions and postconditions, you ensure that each test method is executed in isolation, with a clean and consistent state. 👉Promoting Reusability: Setup and teardown tasks defined in preconditions and postconditions can be reused across multiple test methods, promoting code reuse and reducing redundancy. 👉Maintainability: Clearly defined preconditions and postconditions enhance the maintainability of test code. Changes or additions to the setup and cleanup logic can be made centrally in these annotated methods. TestNG PreConditions and PostConditions flow.   Before Suite (once) Before Test (once for each <test> in the suite) Before Class (once for each class with test methods in the <test> ) Before Method (before each test method) Test method execution After Method (after each test method) After Class (after all test methods in ...

Essential of Creating Test plan in Testing

  Why Test Plan is Vital Document! Test Planning process and plan itself serve as vehicle for communicating with other members of the project team, tester, Dev, BA, Peer and other stakeholders. This communication allows the test plan to influence the project team and allows the project team to influence the test plan. We can accomplish this Communication through circulation of one or more test plan drafts and through review meeting. E.g.: Please tell me what the plan is for releasing the test items into the test lab for each cycle of test execution E.g.: Please let me know which version of the test tool   will be used for the regression tests of the previous increment. A test plan Document becomes the record of previous discussions and agreements. Test Plan helps us to Manage the change if you are in Agile , have to this as Handy. Ways to write a Test Plan Document. Analyze the Product: Understand the product's objectives, target users, specific...