Predicting software defects is a long-standing challenge in software engineering because defects discovered late in the process can be expensive and time consuming to fix. In this study, we introduce a contrastive learning approach that incorporates an inter-sample attention mechanism, inspired by transformer models, to create context-aware representations of software classes. The idea is to not only examine software metrics but also consider how software classes relate to each other within a project, allowing the model to assign weights to features dynamically based on these relationships. We evaluated our method on nine projects from three widely used benchmark datasets (PROMISE, AEEEM, and ReLink) and compared it against five baselines: two recent state-of-the-art transformer-based models designed for tabular data (FT-Transformer and SAINT), and three widely adopted tree-based classifiers (Random Forest, XGBoost, and CatBoost). Our results show that the proposed approach outperforms all five baselines across all nine projects, with consistent improvements in the F1-score across projects with varying defect ratios ranging from 13.1% to 50.5%. These findings suggest that combining contrastive learning with inter-sample attention mechanisms provides a promising method for leveraging the similarities between software classes for defect prediction, primarily relying on software metrics.