SShortSingh.
Back to feed

Java 'new' vs 'newInstance()': Key Differences Every Developer Should Know

0
·2 views

Java offers two primary ways to create objects: the 'new' operator, used when the class is known at compile time, and reflection-based instantiation via 'newInstance()', used when the class name is determined only at runtime. While 'new' directly invokes a constructor and is straightforward, reflection allows dynamic object creation by passing a class name as a string, making programs more flexible. The older 'Class.newInstance()' method was deprecated in Java 9 due to poor exception handling and its reliance on a no-argument constructor. The modern recommended approach uses 'getDeclaredConstructor().newInstance()', which supports parameterized constructors and handles exceptions more reliably. Developers should prefer 'new' for standard use cases and turn to reflection only when runtime flexibility is genuinely required.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Developer Builds 21-Skill Library to Help AI Agents Write Consistent Engineering Docs

A developer frustrated by repeatedly recreating technical documentation from scratch built a tool called engineering-docs, a library of 21 structured skills designed for AI coding agents. The library covers the full software development lifecycle, from initial discovery and planning through to postmortems after production incidents. The creator observed that AI coding agents, much like rushed engineers, tend to produce incomplete outputs — omitting details like rollback plans, threat models, or API versioning strategies — because they lack internalized documentation standards. Rather than crafting better prompts each time, the developer encoded established frameworks such as STRIDE threat modeling, EARS requirements syntax, and the Richardson Maturity Model directly into reusable agent skills. The library is organized into five phases mirroring real project progression, aiming to make thorough engineering documentation consistent and automatic across projects.

0
ProgrammingDEV Community ·

Diff Debt: Why Unread AI-Generated Code Is a Hidden Risk Beyond Technical Debt

Software developer and writer introduced the term 'diff debt' to describe code that enters a codebase without anyone truly understanding it — distinct from technical debt, where developers knowingly choose shortcuts. Technical debt, coined by Ward Cunningham in the early 1990s, implies the author understands what was shipped and can plan to fix it later. Diff debt, by contrast, arises when pull requests are approved without being genuinely read, often because AI-generated code looks clean and passes tests but was never mentally modeled by any human. The rise of AI coding tools has widened this gap, since generating hundreds of lines of code now takes seconds while thorough code review has not become any faster. Unlike technical debt, diff debt is invisible on any project backlog and only surfaces when something breaks and no one on the team can explain the code.

0
ProgrammingDEV Community ·

Silent HTTP 200 Bugs Can Triple Cloud Costs Without Triggering Any Alerts

A class of software bugs causes systems to function normally and return successful HTTP 200 responses while silently inflating cloud infrastructure costs. A real-world example occurred on July 13, 2026, when a liteLLM prompt cache invalidation issue caused prefix-based caching to stop working after message ordering in request payloads was changed. Because all outputs remained correct and latency was unchanged, no monitoring systems flagged the problem, and the full token cost was charged on every API call. Traditional engineering tests focus on output correctness rather than cost efficiency, meaning expense regressions often surface only in monthly finance reports rather than in automated build failures. Experts argue that cost metrics like cache hit rates should be treated as hard engineering constraints, capable of failing a pull request, rather than as optional accounting considerations.

0
ProgrammingDEV Community ·

How Blameless Postmortems Help Engineering Teams Learn From Outages

A structured incident postmortem — also known as a post-incident review — is a formal analysis conducted after a production outage is resolved, aimed at identifying systemic causes rather than assigning individual blame. The blameless postmortem approach was pioneered by John Allspaw and Paul Hammond at Etsy and later codified in Google's Site Reliability Engineering book, which emphasizes that engineers always act on the best information available at the time. Engineering teams are advised to conduct postmortems within 24 to 72 hours of incident resolution, while details remain fresh and corrective actions are still actionable. Key components of an effective postmortem include a detailed timeline, root cause analysis using iterative 'why' questioning, a record of contributing factors, and clearly assigned follow-up actions. The guide also recommends running postmortems for near-miss incidents, noting they offer the same learning value as major outages but without any customer impact.